package cn.superdesk.uniorder.service.fund.impl;

import cn.superdesk.uniorder.common.exception.UnifiedOrderRuntimeException;
import cn.superdesk.uniorder.common.exception.constants.ExceptionMsg;
import cn.superdesk.uniorder.common.numerical.AnDateHelper;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.JwtUtil;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.fund.SysFundUser;
import cn.superdesk.uniorder.dao.entity.fund.SysFundUserExample;
import cn.superdesk.uniorder.dao.mapper.fund.SysFundUserMapper;
import cn.superdesk.uniorder.dto.vo.fund.SysFundUserVoEx;
import cn.superdesk.uniorder.service.fund.SysFundUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysFundUserServiceImpl implements SysFundUserService {

    @Autowired
    private SysFundUserMapper sysFundUserMapper;


    @Override
    public int insertSelective(SysFundUser sysFundUser) {
        SysFundUserExample example = new SysFundUserExample();
        example.or().andOrganizationIdEqualTo(sysFundUser.getOrganizationId());
        if (sysFundUserMapper.countByExample(example) > 0) {
            throw new UnifiedOrderRuntimeException(ExceptionMsg.SYS_ERROR.getCode(), "该项目已存在采购基金管理员");
        }
        sysFundUser.setCreateTime(AnDateHelper.getNow());
        return sysFundUserMapper.insertSelective(sysFundUser);
    }

    @Override
    public int updateByPrimaryKeySelective(SysFundUser sysFundUser) {
        sysFundUser.setUpdateTime(AnDateHelper.getNow());
        return sysFundUserMapper.updateByPrimaryKeySelective(sysFundUser);
    }

    @Override
    public void deleteObj(Long id) {

    }

    @Override
    public SysFundUser findById(Long id) {
        return sysFundUserMapper.selectByPrimaryKey(id);
    }


    @Override
    public List<SysFundUser> findAll(Map<String, Object> queryMap) {
        SysFundUser sysFundUser = new SysFundUser();
        return null;
    }

    @Override
    public PageInfo<SysFundUser> findByPaging(Map<String, Object> params, Integer pageNo, Integer pageSize) {
        return null;
    }

    @Override
    public void create(SysFundUser sysFundUser, String token) throws UnifiedOrderRuntimeException {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        SysFundUserExample example = new SysFundUserExample();
        example.or().andOrganizationIdEqualTo(sysFundUser.getOrganizationId());

        if (sysFundUserMapper.countByExample(example) > 0) {
            throw new UnifiedOrderRuntimeException(ExceptionMsg.SYS_ERROR.getCode(), "该项目已存在采购基金管理员");
        }

        sysFundUser.setCreateUserId(loginSysUser.getSysUserId());
        sysFundUser.setCreateTime(AnDateHelper.getNow());
        sysFundUserMapper.insertSelective(sysFundUser);
    }

    @Override
    public void update(SysFundUser sysFundUser, String token) {

        LoginSysUser loginSysUser = JwtUtil.getLoginSysUser(token);

        sysFundUser.setUpdateUserId(loginSysUser.getSysUserId());
        sysFundUser.setUpdateTime(AnDateHelper.getNow());
        sysFundUserMapper.updateByPrimaryKeySelective(sysFundUser);
    }

    @Override
    public R<List<SysFundUserVoEx>> findAllViewPage(Map<String, Object> params, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum, pageSize);
        List<SysFundUserVoEx>     ls   = sysFundUserMapper.selectByCondition(params);
        PageInfo<SysFundUserVoEx> page = new PageInfo<SysFundUserVoEx>(ls);
        PageHelper.clearPage();
        return new R<List<SysFundUserVoEx>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), ls);
    }

    @Override
    public SysFundUserVoEx selectById(Map<String, Object> params) throws UnifiedOrderRuntimeException {

        List<SysFundUserVoEx> ls = sysFundUserMapper.selectByCondition(params);

        if (ls == null || ls.size() == 0) {
            throw new UnifiedOrderRuntimeException(ExceptionMsg.SYS_ERROR.getCode(), "该id不存在");
        }

        return ls.get(0);
    }
}
