package cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfo;
import cn.superdesk.uniorder.dao.entity.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoExample;
import cn.superdesk.uniorder.dao.mapper.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoMapper;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoQueryCriteria;
import cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExtendInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2022-03-17 13:52:19
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022-03-17 13:52:19
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service
public class SzhtxxElectronicInvoiceFptxxExtendInfoServiceImpl implements SzhtxxElectronicInvoiceFptxxExtendInfoService {

    @Autowired
    private SzhtxxElectronicInvoiceFptxxExtendInfoMapper szhtxxElectronicInvoiceFptxxExtendInfoMapper;


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

    @Override
    public int insertSelective(SzhtxxElectronicInvoiceFptxxExtendInfo entity) {
        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.insertSelective(entity);
    }

//    public int addSzhtxxElectronicInvoiceFptxxExtendInfo(SzhtxxElectronicInvoiceFptxxExtendInfo entity) {
//        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.insertSelective(entity);
//    }

    @Override
    public int updateByPrimaryKeySelective(SzhtxxElectronicInvoiceFptxxExtendInfo entity) {
        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.updateByPrimaryKeySelective(entity);
    }

//    public int updateSzhtxxElectronicInvoiceFptxxExtendInfo(SzhtxxElectronicInvoiceFptxxExtendInfo entity) {
//
//        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.updateByPrimaryKeySelective(entity);
//    }
//
//    public int updateByPrimaryKeySelectiveWithRet(SzhtxxElectronicInvoiceFptxxExtendInfo entity) {
//
//        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.updateByPrimaryKeySelective(entity);
//    }

    @Override
    public void deleteObj(Long id) {
    }

    @Override
    public SzhtxxElectronicInvoiceFptxxExtendInfo findById(Long id) {
        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxxExtendInfo findByFpqqlsh(String fpqqlsh) {

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

        criteria.andFpqqlshEqualTo(fpqqlsh);

        List<SzhtxxElectronicInvoiceFptxxExtendInfo> resultList = szhtxxElectronicInvoiceFptxxExtendInfoMapper.selectByExample(example);

        if (CollUtil.isNotEmpty(resultList)) {
            return resultList.get(0);
        } else {
            return null;
        }

    }

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

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

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

    public int saveOrUpdateByFpqqlsh(SzhtxxElectronicInvoiceFptxxExtendInfo entity) {

        if (ObjectUtil.isEmpty(entity.getFpqqlsh())) {
            return 0;
        }

        SzhtxxElectronicInvoiceFptxxExtendInfo originEntity = findByFpqqlsh(entity.getFpqqlsh());

        if (ObjectUtil.isNotNull(originEntity)) {
            entity.setId(originEntity.getId());
            return updateByPrimaryKeySelective(entity);
        } else {
            return insertSelective(entity);
        }

    }


    /**
     * 【【结算中心-测试环境】积分结算-金税盘发票-CAA2022051097499392-回款登记后更新时间未变化】
     * https://www.tapd.cn/33003868/bugtrace/bugs/view?bug_id=1133003868001005099
     *
     * @param loginSysUser
     * @param now
     * @return
     */
    public int updateUpdateUserInfo(String fpqqlsh, LoginSysUser loginSysUser, DateTime now) {

        int ret = 0;

        SzhtxxElectronicInvoiceFptxxExtendInfo entityExistDb = findByFpqqlsh(fpqqlsh);

        if (ObjectUtil.isNotNull(entityExistDb)) {
            ret = updateByPrimaryKeySelective(
                    SzhtxxElectronicInvoiceFptxxExtendInfo
                            .builder()
                            .id(entityExistDb.getId())

                            .updateTime(now)
                            .updateUserId(loginSysUser.getSysUserId())
                            .updateUserName(loginSysUser.getFullName())
                            .build()
            );
        }

        return ret;
    }

    public int deleteSzhtxxElectronicInvoiceFptxxExtendInfo(Long id) {
        return szhtxxElectronicInvoiceFptxxExtendInfoMapper.deleteByPrimaryKey(id);
    }

    public SzhtxxElectronicInvoiceFptxxExtendInfo findSzhtxxElectronicInvoiceFptxxExtendInfoById(Long id) {
        SzhtxxElectronicInvoiceFptxxExtendInfo entity = szhtxxElectronicInvoiceFptxxExtendInfoMapper.selectByPrimaryKey(id);
        return entity;
    }

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

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

        // TODO 喜欢用Map 还是 Bean 自己喜欢... 后期添加 转 SzhtxxElectronicInvoiceFptxxExtendInfoExample.Criteria
        // SzhtxxElectronicInvoiceFptxxExtendInfo szhtxxElectronicInvoiceFptxxExtendInfoConditions = BeanUtils.mapToBean(conditions,SzhtxxElectronicInvoiceFptxxExtendInfo.class);

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

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

    }

    public R<List<SzhtxxElectronicInvoiceFptxxExtendInfo>> pageQueryGenericParam(SzhtxxElectronicInvoiceFptxxExtendInfoQueryCriteria queryCriteria) {

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

        // TODO 添加条件... 后期添加 queryCriteria 转 SzhtxxElectronicInvoiceFptxxExtendInfoExample.Criteria

        PageHelper.startPage(queryCriteria.getPageNumber(), queryCriteria.getPageSize());
        List<SzhtxxElectronicInvoiceFptxxExtendInfo>     resultList = szhtxxElectronicInvoiceFptxxExtendInfoMapper.selectByExample(example);
        PageInfo<SzhtxxElectronicInvoiceFptxxExtendInfo> page       = new PageInfo<SzhtxxElectronicInvoiceFptxxExtendInfo>(resultList);
        PageHelper.clearPage();

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

    }

}
