package com.unis.crk.cplbusiness.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.cplbusiness.entity.DataCrkywCplrkDefault;
import com.unis.crk.cplbusiness.entity.SettlementManagement;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplrkDefaultVo;
import com.unis.crk.cplbusiness.entity.vo.SettlementManagementVo;
import com.unis.crk.cplbusiness.mapper.SettlementManagementMapper;
import com.unis.crk.cplbusiness.service.IDataCrkywCplrkDefaultService;
import com.unis.crk.cplbusiness.service.ISettlementManagementService;
import com.unis.crk.utils.ResultUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 结算管理 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-05-23
 */
@Service
public class SettlementManagementServiceImpl extends ServiceImpl<SettlementManagementMapper, SettlementManagement> implements ISettlementManagementService {

    @Resource
    private SettlementManagementMapper settlementManagementMapper;
    @Autowired
    private IDataCrkywCplrkDefaultService dataCrkywCplrkDefaultService;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;
    /**
     *修改结算管理
     * @param param 结算管理业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(SettlementManagement param){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        if(!super.saveOrUpdate(param)){
            jsonObject.set("msg", "修改数据失败");
            return jsonObject;
        }
        jsonObject.set("success", true);
        jsonObject.set("msg", "修改成功");
        return jsonObject;
    }
    /**
     *删除结算管理数据
     * @param id 结算管理主键尖
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject delete(String id){
        JSONObject jsonObject = new JSONObject();
        jsonObject.set("success",false);
        SettlementManagement settlementManagement = settlementManagementMapper.selectById(id);
        if(!super.removeById(id)){
            jsonObject.set("msg", "删除数据失败");
            return jsonObject;
        }
        deleteDataRecordService.save(id,"settlement_management",settlementManagement.getUnitid(),new Date());
        jsonObject.set("success", true);
        jsonObject.set("msg", "删除成功");
        return jsonObject;
        }

    /**
     * 按照条件查询结算管理信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(SettlementManagementVo pageQuery){
        {
        //条形查询器
        QueryWrapper<SettlementManagement> queryWrapper = new QueryWrapper<>();
        //分页相关
        Page<SettlementManagement> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());
            if(StringUtils.isNotBlank(pageQuery.getUnitId())) {
                queryWrapper.eq("unitid", pageQuery.getUnitId());
            }
            if(StringUtils.isNotBlank(pageQuery.getAuditStatus())){
                queryWrapper.eq("audit_status",pageQuery.getAuditStatus());
            }
            if(StrUtil.isNotBlank(pageQuery.getReceiptDocNo())){
                queryWrapper.like("receipt_doc_no", ResultUtil.getQueryWrapperformat(pageQuery.getReceiptDocNo()));

            }
            if(StrUtil.isNotBlank(pageQuery.getTradeName())){
                queryWrapper.like("trade_name",ResultUtil.getQueryWrapperformat(pageQuery.getTradeName()));
            }

            if (StrUtil.isNotBlank(pageQuery.getYwStart()) && StrUtil.isNotBlank(pageQuery.getYwEnd())){
                queryWrapper.apply(" DATE_FORMAT(ifnull(audit_time,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getYwStart()+"' AND DATE_FORMAT(ifnull(audit_time,NOW()),'%Y-%m-%d') <= '"+pageQuery.getYwEnd()+" 23:59:59'");

            }
            if (StrUtil.isNotBlank(pageQuery.getRKywStart()) && StrUtil.isNotBlank(pageQuery.getRKywEnd())){
                queryWrapper.apply(" DATE_FORMAT(ifnull(warehousing_time,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getRKywStart()+"' AND DATE_FORMAT(ifnull(warehousing_time,NOW()),'%Y-%m-%d') <= '"+pageQuery.getRKywEnd()+" 23:59:59'");

            }
            queryWrapper.orderByDesc("builderDate");
            IPage<Map<String,Object>> page = settlementManagementMapper.selectMyMapsPage(p,queryWrapper);
            return page;
        }
    }

    @Override
    public DataCrkywCplrkDefault getByRkdh(String rkdh) {
        QueryWrapper<DataCrkywCplrkDefault> dataCrkywCplrkDefault=new QueryWrapper<>();
        dataCrkywCplrkDefault.eq("rkbh",rkdh);
        DataCrkywCplrkDefault one = dataCrkywCplrkDefaultService.getOne(dataCrkywCplrkDefault);
        DataCrkywCplrkDefault oneData = dataCrkywCplrkDefaultService.getOneData(one.getDataId());
        return oneData;
    }

    @Override
    public SettlementManagement getOneData(String id) {
        SettlementManagement settlementManagement = this.settlementManagementMapper.selectById(id);
        String receiptDocNo = settlementManagement.getReceiptDocNo();
        QueryWrapper<DataCrkywCplrkDefault> dataCrkywCplrkDefault=new QueryWrapper<>();
        dataCrkywCplrkDefault.eq("rkbh",receiptDocNo);
        DataCrkywCplrkDefault one = dataCrkywCplrkDefaultService.getOne(dataCrkywCplrkDefault);
        if(ObjectUtil.isNotNull(one)){
            DataCrkywCplrkDefault oneData = dataCrkywCplrkDefaultService.getOneData(one.getDataId());
            settlementManagement.setRk(oneData);
        }
        return settlementManagement;
    }


}
