package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.PositionDto;
import com.sptg.estate.api.entity.Position;
import com.sptg.estate.api.entity.PositionRecord;
import com.sptg.estate.api.entity.ProjectCompletion;
import com.sptg.estate.biz.mapper.PositionMapper;
import com.sptg.estate.biz.service.PositionRecordServiceInterface;
import com.sptg.estate.biz.service.PositionServiceInterface;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * title: ProjectServiceImpl
 * package: com.sptg.estate.biz.security
 * description: 实施项目定位-服务层实现层
 *
 *               
 *                     
 */
@Service
@RequiredArgsConstructor
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionServiceInterface {

    private final PositionRecordServiceInterface positionRecordServiceInterface;

    @Override
    public Page<Position> showList(PositionDto dto){
        Page<Position> page=dto.getPage();
        EntityWrapper<Position> wrapper=new EntityWrapper<Position>();
        //楼盘名称
        String residentialName = dto.getResidentialName();
        if(StringUtils.isNotBlank(residentialName)){
            wrapper.like("s_residential_name",residentialName);
        }
        //小区id
        if(dto.getResidentialId()!=null){
            wrapper.eq("l_residential_id",dto.getResidentialId());
        }
        //工单审核状态：0-待审核 1-审核通过 2-审核不通过
        Integer checkState = dto.getCheckState();
        if(checkState!=null){
            wrapper.eq("i_check_state",checkState);
        }
        //定位人
        String createName = dto.getCreateName();
        if(StringUtils.isNotBlank(createName)){
            wrapper.like("s_create_name",createName);
        }
        //开始定位时间
        Date beginDate = dto.getBeginDate();
        if(beginDate!=null){
            wrapper.ge("t_create_date",beginDate);
        }
        //结束定位时间
        Date endDate = dto.getEndDate();
        if(endDate!=null){
            wrapper.le("t_create_date",endDate);
        }
        //位置
        String orgPosition = dto.getOrgPosition();
        if(StringUtils.isNotBlank(orgPosition)){
            wrapper.like("s_org_position",orgPosition, SqlLike.RIGHT);
        }
        //排序
        wrapper.orderBy("t_create_date",false);
        Page<Position> positionPage = this.selectPage(page, wrapper);
        return positionPage;
    }

    @Override
    public ResultBean<Position> findByProjectId(Long projectId) {
        Wrapper<Position> wrapper = new EntityWrapper<>();
        wrapper.eq("l_project_id",projectId);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectOne(wrapper));
    }

    @Override
    public ResultBean<Integer> countByOrgPosition(String orgPosition) {
        Wrapper<Position> wrapper = new EntityWrapper<>();
        wrapper.like("s_org_position", orgPosition);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectCount(wrapper));
    }

    @Override
    public boolean checkState(PositionDto dto) {

        if(dto==null||dto.getId()==null||dto.getCheckState()==null){
            return false;
        }
//        //判断是否可审核
//        Position currentPosition = this.selectById(dto.getId());
//        if(currentPosition==null||currentPosition.getCheckState()!=0){
//            return false;
//        }
        Position position=new Position();
        position.setId(dto.getId());
        if(dto.getCheckState()==OperationConstant.TWO){
            position.setCheckOpinion(dto.getCheckOpinion());
        }
        position.setCheckDate(new Date());
        position.setCheckAccountId(dto.getCheckAccountId());
        position.setCheckName(dto.getCheckName());
        position.setCheckState(dto.getCheckState());
        boolean result = this.updateById(position);
        return result;
    }

    @Override
    public ResultBean<Position> findListByPosition(PositionDto dto) {
        Wrapper<Position> wrapper = new EntityWrapper<>();
        //机构结构
        wrapper.like("s_org_position", dto.getOrgPosition());
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectList(wrapper));
    }

    @Override
    public ResultBean<Boolean> editEntity(PositionDto dto){
        List<PositionRecord> list = dto.getPositionRecords();
        Integer pillar = 0;
        Integer wallHung = 0;
        Integer rainCover = 0;
        for(PositionRecord p : list){
            if(p.getDeviceType()==0){
                pillar++;
            }else if(p.getDeviceType()==1){
                wallHung++;
            }else if(p.getDeviceType()==2){
                rainCover++;
            }
        }
        dto.setPillar(pillar);
        dto.setWallHung(wallHung);
        dto.setRainCover(rainCover);
        Boolean result = this.updateById(dto);
        for(PositionRecord p : list){
            if(p.getId()!=null){
                this.positionRecordServiceInterface.updateById(p);
            }else{
                p.setPositionId(dto.getId());
                this.positionRecordServiceInterface.insert(p);
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    public ResultBean<Boolean> countByProjectId(Long projectId) {
        Wrapper<Position> wrapper = new EntityWrapper<>();
        //项目id
        wrapper.eq("l_project_id", projectId);
        int count = this.selectCount(wrapper);
        if(count>0){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }
        return new ResultBean<>(ResultEnum.SUCCESS,false);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public ResultBean<Boolean> insertEntity(PositionDto dto) {
        if(this.countByProjectId(dto.getProjectId()).getData()){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }else{
            List<PositionRecord> list = dto.getPositionRecords();
            Integer pillar = 0;
            Integer wallHung = 0;
            Integer rainCover = 0;
            for(PositionRecord p : list){
                if(p.getDeviceType()==0){
                    pillar++;
                }else if(p.getDeviceType()==1){
                    wallHung++;
                }else if(p.getDeviceType()==2){
                    rainCover++;
                }
            }
            dto.setPillar(pillar);
            dto.setWallHung(wallHung);
            dto.setRainCover(rainCover);
            //生成订单号
            //获取时间对象
            Date date = dto.getCreateDate();
            //获取日期字符串
            String s = new SimpleDateFormat("yyMMdd").format(date);
            Wrapper<Position> wrapper = new EntityWrapper<>();
            wrapper.like("s_order_no",s);
            //统计当天新增的定位数
            int count = this.selectCount(wrapper);
            //订单编号的格式用yyMMdd+2位排序号前面补0
            dto.setOrderNo(s+(count<9?("0"+(count+1)):++count));
            Boolean result = this.insert(dto);
            if(result){
                for(PositionRecord p : list){
                    p.setPositionId(dto.getId());
                }
                result = this.positionRecordServiceInterface.insertBatch(list);
            }
            return new ResultBean<>(ResultEnum.SUCCESS,result);
        }
    }
}
