package cn.superdesk.uniorder.service.sys.notify.impl;

import cn.hutool.core.date.DateUtil;
import cn.superdesk.uniorder.common.enums.sys.notify.SysNotifyCategoryDefaultEnum;
import cn.superdesk.uniorder.common.enums.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceResponseCodeEnum;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.numerical.AnDateHelper;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotifyExample;
import cn.superdesk.uniorder.dao.mapper.sys.notify.SysNotifyMapper;
import cn.superdesk.uniorder.dto.param.system.notify.SysNotifyClearByDatabaseRecordCriteria;
import cn.superdesk.uniorder.service.sys.notify.SysNotifyService;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2021-03-01 17:37:17
 * @UpdateUser: linjinyu
 * @UpdateDate: 2021-03-01 17:37:17
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Service
public class SysNotifyServiceImpl implements SysNotifyService {

    @Autowired
    private SysNotifyMapper sysNotifyMapper;


    /****************************************** extends BaseService 部分 start ******************************************/


    @Override
    public int insertSelective(SysNotify entity) {
        return sysNotifyMapper.insertSelective(entity);
    }

    @Override
    public int updateByPrimaryKeySelective(SysNotify entity) {
        return sysNotifyMapper.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SysNotify findById(Long id) {
        return sysNotifyMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysNotify> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return null;
    }

    @Override
    public List<SysNotify> findAll(Map<String, Object> queryMap) {
        return null;
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    public void addSysNotify(SysNotify entity) {
        sysNotifyMapper.insertSelective(entity);
    }

    public void updateSysNotify(SysNotify entity) {
        SysNotify originEntity = sysNotifyMapper.selectByPrimaryKey(entity.getId());
//		AssertUtil.notNull(originEntity);
        AnBeanUtils.copy(entity, originEntity);
        sysNotifyMapper.updateByPrimaryKeySelective(originEntity);
    }

    public void deleteSysNotify(Long id) {
        sysNotifyMapper.deleteByPrimaryKey(id);
    }

    public SysNotify findSysNotifyById(Long id) {
        SysNotify entity = sysNotifyMapper.selectByPrimaryKey(id);
//		AssertUtil.notNull(entity);
        return entity;
    }

    public R<List<SysNotify>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize, LoginSysUser loginSysUser) {

        // 样例 保留
//		SysNotify sysNotifyConditions = AnBeanUtils.mapToBean(conditions,SysNotify.class);

        SysNotifyExample          example  = new SysNotifyExample();
        SysNotifyExample.Criteria criteria = example.createCriteria();

        criteria
                .andCategoryIdEqualTo(SysNotifyCategoryDefaultEnum.导出通知.getKey())
                .andToTimeGreaterThanOrEqualTo(AnDateHelper.getDateByOffsetDay(DateUtil.date(), -7))
                .andToUserIdEqualTo(loginSysUser.getSysUserId());

        example.setOrderByClause("to_time DESC");

        PageHelper.startPage(pageNun, pageSize);
        List<SysNotify>     resultList = sysNotifyMapper.selectByExample(example);
        PageInfo<SysNotify> page       = new PageInfo<SysNotify>(resultList);
        PageHelper.clearPage();

        return new R<List<SysNotify>>(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), resultList);

    }

    /**
     * 为了清理用的，找出7天前清理的
     *
     * @param sysNotifyClearByDatabaseRecordCriteria
     * @return
     */
    public List<SysNotify> findByClearByDatabaseRecord(SysNotifyClearByDatabaseRecordCriteria sysNotifyClearByDatabaseRecordCriteria) {

        List<SysNotify> resultList = new ArrayList<>();

        SysNotifyExample          example  = new SysNotifyExample();
        SysNotifyExample.Criteria criteria = example.createCriteria();

        criteria
                .andToTimeLessThanOrEqualTo(AnDateHelper.getDateByOffsetDay(DateUtil.date(), -7));
        example.setOrderByClause("to_time DESC");

        resultList = sysNotifyMapper.selectByExampleWithBLOBs(example);

        return resultList;

    }


    /****************************************** 常规返回 start ******************************************/


    public SysNotify receiveInvoiceApplyCustomerFieldMakeInvoiceStatusSuccess(LoginSysUser loginSysUser) {

        return SysNotify.builder()
                .title(SzhtxxElectronicInvoiceResponseCodeEnum.SUCCESS.getReturnCode())
                .content("操作成功")
                .build();
    }

}
