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

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.business.qcrk.entity.DataCrkywCmzDefault;
import com.unis.crk.common.service.IDeleteDataRecordService;
import com.unis.crk.cplbusiness.entity.DataCrkywCplbjdyDefault;
import com.unis.crk.cplbusiness.entity.DataCrkywCplckDefault;
import com.unis.crk.cplbusiness.entity.DataCrkywCplckFlt001;
import com.unis.crk.cplbusiness.entity.vo.DataCrkywCplbjdyDefaultVo;
import com.unis.crk.cplbusiness.mapper.DataCrkywCplbjdyDefaultMapper;
import com.unis.crk.cplbusiness.service.IDataCrkywCplbjdyDefaultService;
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.List;
import java.util.Map;

/**
 * <p>
 * 标签打印 服务实现类
 * </p>
 *
 * @author TangSong
 * @since 2022-04-28
 */
@Service
public class DataCrkywCplbjdyDefaultServiceImpl extends ServiceImpl<DataCrkywCplbjdyDefaultMapper, DataCrkywCplbjdyDefault> implements IDataCrkywCplbjdyDefaultService {

    @Resource
    private DataCrkywCplbjdyDefaultMapper dataCrkywCplbjdyDefaultMapper;
    @Autowired
    private IDeleteDataRecordService deleteDataRecordService;
    /**
     *保存标签打印
     * @param param 标签打印业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject saveBjdy(DataCrkywCplbjdyDefault param){
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("success", false);
            param.setDeptId(param.getUnitid());//部门id
            param.setCjsj(new Date());
            if (StringUtils.isBlank(param.getBuilder())){
                param.setBuilder(param.getUnitid());
                param.setEditor(param.getUnitid());
            }
            if (!super.saveOrUpdate(param)) { //
                jsonObject.set("msg", "保存失败");
                return jsonObject;
            }
            jsonObject.set("success", true);
            jsonObject.set("msg", "保存成功");
            return jsonObject;
    }
    /**
     *修改标签打印
     * @param param 标签打印业务数据
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public JSONObject update(DataCrkywCplbjdyDefault param){
        {
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("success", false);
            param.setDeptId(param.getUnitid());//部门id
            param.setModifyDate(new Date());
            if (StringUtils.isBlank(param.getBuilder())){
                param.setEditor(param.getUnitid());
            }
            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);
            //得到对象
            DataCrkywCplbjdyDefault entity = super.getById(id);
            if (entity == null) {
                jsonObject.set("msg", "此数据已被删除");
                return jsonObject;
            }
            if (!super.removeById(id)) {
                jsonObject.set("msg", "删除失败");
                return jsonObject;
            }
            deleteDataRecordService.save(id,"data_crkyw_cplbjdy_default",entity.getUnitid(),new Date());
            jsonObject.set("success", true);
            jsonObject.set("msg", "删除成功");
            return jsonObject;
        }
    }

    /**
     * 按照条件查询标签打印信息
     * @param pageQuery
     * @throws Exception
     */
    @Override
    public IPage<Map<String,Object>> findPageVo(DataCrkywCplbjdyDefaultVo pageQuery){
        {
        //条形查询器
        QueryWrapper<DataCrkywCplbjdyDefault> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(pageQuery.getUnitId())){
          queryWrapper.eq("a.unitId",pageQuery.getUnitId());
        }
        if(StringUtils.isNotBlank(pageQuery.getSjjlspmc())){
            queryWrapper.like("a.sjjlspmc", ResultUtil.getQueryWrapperformat(pageQuery.getSjjlspmc()));
        }
        if(StringUtils.isNotBlank(pageQuery.getYwlx())){
            queryWrapper.like("a.ywlx",pageQuery.getYwlx());
        }
        if (StringUtils.isNotBlank(pageQuery.getYwsjStart()) && StringUtils.isNotBlank(pageQuery.getYwsjEnd())){
            queryWrapper.apply(" DATE_FORMAT(ifnull(ywsj,NOW()),'%Y-%m-%d') >=  '"+pageQuery.getYwsjStart()+"' AND DATE_FORMAT(ifnull(ywsj,NOW()),'%Y-%m-%d') <= '"+pageQuery.getYwsjEnd()+" 23:59:59'");

        }
        //分页相关
        Page<DataCrkywCplbjdyDefault> p =new Page<>(pageQuery.getPage(),pageQuery.getLimit());

        IPage<Map<String,Object>> page = dataCrkywCplbjdyDefaultMapper.selectMyMapsPage(p,queryWrapper);
        return page;
        }
    }

    /**
     * 根据标签号查询数据
     *
     * @param bjbh
     * @return
     */
    @Override
    public DataCrkywCplbjdyDefault getOneBybh(String bjbh) {
            QueryWrapper<DataCrkywCplbjdyDefault> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("bjbh", bjbh);
            queryWrapper.last("limit 1");
            DataCrkywCplbjdyDefault dataCrkywCplbjdyDefault = dataCrkywCplbjdyDefaultMapper.selectOne(queryWrapper);
            return dataCrkywCplbjdyDefault;
    }

    /**
     * 根据标签号查询数据
     *
     * @param bjbhs
     * @return
     */
    @Override
    public List<DataCrkywCplbjdyDefault> getListBybhs(String bjbhs) {
        if (StrUtil.isNotBlank(bjbhs)){
            QueryWrapper<DataCrkywCplbjdyDefault> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("bjbh", StrUtil.split(bjbhs,","));
            return super.list(queryWrapper) ;
        }
        return  null;
    }

}
