package com.dcsoft.system.consume.service.impl;

import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.system.consume.domain.ConsumeRecord;
import com.dcsoft.system.consume.domain.ConsumeSubsidyRecord;
import com.dcsoft.system.consume.mapper.ConsumeRecordMapper;
import com.dcsoft.system.consume.mapper.ConsumeSubsidyRecordMapper;
import com.dcsoft.system.consume.service.IConsumeSubsidyRecordService;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.mapper.SysPeopleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


/**
 * 餐补明细Service业务层处理
 *
 * @author nichun
 * @date 2023-10-11
 */
@Service
public class ConsumeSubsidyRecordServiceImpl implements IConsumeSubsidyRecordService
{
    @Autowired
    private ConsumeSubsidyRecordMapper consumeSubsidyRecordMapper;

    @Autowired
    private ConsumeRecordMapper consumeRecordMapper;
/*
    @Autowired
    private HkPeopleRecordMapper hkPeopleRecordMapper;*/

    @Autowired
    private SysPeopleMapper sysPeopleMapper;

    /**
     * 查询餐补明细
     *
     * @param id 餐补明细主键
     * @return 餐补明细
     */
    @Override
    public ConsumeSubsidyRecord selectConsumeSubsidyRecordById(Long id)
    {
        return consumeSubsidyRecordMapper.selectConsumeSubsidyRecordById(id);
    }

    /**
     * 查询餐补明细列表
     *
     * @param consumeSubsidyRecord 餐补明细
     * @return 餐补明细
     */
    @Override
    public List<ConsumeSubsidyRecord> selectConsumeSubsidyRecordList(ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        return consumeSubsidyRecordMapper.selectConsumeSubsidyRecordList(consumeSubsidyRecord);
    }

    /**
     * 新增餐补明细
     *
     * @param consumeSubsidyRecord 餐补明细
     * @return 结果
     */
    @Override
    public int insertConsumeSubsidyRecord(ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        consumeSubsidyRecord.setCreateTime(DateUtils.getNowDate());
        return consumeSubsidyRecordMapper.insertConsumeSubsidyRecord(consumeSubsidyRecord);
    }

    /**
     * 修改餐补明细
     *
     * @param consumeSubsidyRecord 餐补明细
     * @return 结果
     */
    @Override
    public int updateConsumeSubsidyRecord(ConsumeSubsidyRecord consumeSubsidyRecord)
    {
        consumeSubsidyRecord.setUpdateTime(DateUtils.getNowDate());
        return consumeSubsidyRecordMapper.updateConsumeSubsidyRecord(consumeSubsidyRecord);
    }

    /**
     * 批量删除餐补明细
     *
     * @param ids 需要删除的餐补明细主键
     * @return 结果
     */
    @Override
    public int deleteConsumeSubsidyRecordByIds(Long[] ids)
    {
        return consumeSubsidyRecordMapper.deleteConsumeSubsidyRecordByIds(ids);
    }

    /**
     * 删除餐补明细信息
     *
     * @param id 餐补明细主键
     * @return 结果
     */
    @Override
    public int deleteConsumeSubsidyRecordById(Long id)
    {
        return consumeSubsidyRecordMapper.deleteConsumeSubsidyRecordById(id);
    }

    @Override
    public int createConsumeSubsidy(ConsumeSubsidyRecord consumeSubsidyRecord) {
        return 0;
    }

    /*@Override
    public int createConsumeSubsidy(ConsumeSubsidyRecord consumeSubsidyRecord) {
        //查询时间段内消费记录
        //消费记录
        ConsumeRecord consumeRecord=new ConsumeRecord();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取前月的第一天
        Calendar cale = Calendar.getInstance();
        cale.setTime(consumeSubsidyRecord.getSubsidyTime());
        cale.add(Calendar.MONTH,-1);
        cale.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String lastDay = format.format(cale.getTime());
        //获取当月第一天
        Calendar ca = Calendar.getInstance();
        ca.setTime(consumeSubsidyRecord.getSubsidyTime());
        ca.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        String last = format.format(ca.getTime());
        //2023.9.1 02:00:00   - 2023.10.1 02:00:00     计算时间 2023.10.1
        //查询消费人员
        SysPeople peoples=new SysPeople();
        peoples.setIsConsume("1");
        List<SysPeople> peopleList=sysPeopleMapper.selectSysPeopleList(peoples);
        for(SysPeople people:peopleList){
            if(!"无补贴".equals(people.getPosition())&&people.getBranchId()!=1213L){
                consumeRecord.setStartTime(lastDay+" 02:00:00");
                consumeRecord.setEndTime(last+" 02:00:00");
                consumeRecord.setConsumeResult("0");
                consumeRecord.setAccountId(people.getId()+"");
                List<ConsumeRecord> consumeRecordList=consumeRecordMapper.selectConsumeRecordList(consumeRecord);
                //补贴天数
                Long subsidyDay=0L;
                //加班补贴天数
                Long overtime=0L;
                //二维码消费次数
                Long ewmNum=0L;
                Double ewmMoney= Double.valueOf(0);
                for(ConsumeRecord record:consumeRecordList){
                        //中
                        //判断是否存在该时间段的记录，且补助过，有就过
                        if("1".equals(record.getTmrtype())){
                            ConsumeRecord r=new ConsumeRecord();
                            r.setTmrtype("1");
                            r.setConsumeResult("0");
                            r.setStartTime(format.format(record.getStartDate())+" 10:00:00");
                            r.setEndTime(format.format(record.getStartDate())+" 15:00:00");
                            r.setAccountId(people.getId()+"");
                            List<ConsumeRecord> consumeRecordLists=consumeRecordMapper.selectConsumeRecordList(r);
                            if(consumeRecordLists.size()>0){
                                if(record.getId().equals(consumeRecordLists.get(0).getId())){ //判断是否考勤(当天-消费之前有打卡记录)          +补贴天数1天
                                    try {
                                        if("4".equals(record.getPayMode())){
                                            ewmNum++;
                                            ewmMoney+=Double.parseDouble(record.getConsumeMoney());
                                        }else{
                                            subsidyDay++;
                                        }
                                    } catch (Exception e) {
                                        throw new RuntimeException(e);
                                    }
                                }
                            }
                        }
                        //晚
                        //判断是否存在该时间段的记录，且补助过，有就过
                        //判断是否8点后是否有考勤，有考勤     +加班补贴次数
                        if("2".equals(record.getTmrtype())) {
                            ConsumeRecord r=new ConsumeRecord();
                            r.setTmrtype("2");
                            r.setConsumeResult("0");
                            r.setStartTime(format.format(record.getStartDate())+" 15:00:00");
                            r.setEndTime(format.format(record.getStartDate())+" 21:00:00");
                            r.setAccountId(people.getId()+"");
                            List<ConsumeRecord> consumeRecordLists=consumeRecordMapper.selectConsumeRecordList(r);
                            if(consumeRecordLists.size()>0) {
                                if(record.getId().equals(consumeRecordLists.get(0).getId())){
                                    if("1".equals(people.getBranch().getXfbt())){//总部人员补贴上限2次
                                        try {
                                            Date startTime = sdf.parse(format.format(record.getCreateTime())+" 20:00:00");
                                            Date endTime = sdf.parse(format.format(record.getCreateTime())+" 23:59:59");
                                            HkPeopleRecord hkPeopleRecord=new HkPeopleRecord();
                                            hkPeopleRecord.setEmployeeNo(people.getGuid());
                                            hkPeopleRecord.setStartTime(startTime);
                                            hkPeopleRecord.setEndTime(endTime);
                                            List<HkPeopleRecord> list= hkPeopleRecordMapper.selectHkPeopleRecordList(hkPeopleRecord);
                                            if(list.size()>0) {
                                                if("4".equals(record.getPayMode())){
                                                    ewmNum++;
                                                    ewmMoney+=Double.parseDouble(record.getConsumeMoney());
                                                }else{
                                                    overtime++;
                                                }
                                            }
                                        } catch (ParseException e) {
                                            throw new RuntimeException(e);
                                        }
                                    }else{
                                        //判断中餐有消费就不算晚餐，其他人员补贴次数一次
                                        ConsumeRecord rs=new ConsumeRecord();
                                        rs.setTmrtype("1");
                                        rs.setConsumeResult("0");
                                        rs.setStartTime(format.format(record.getStartDate())+" 10:00:00");
                                        rs.setEndTime(format.format(record.getStartDate())+" 15:00:00");
                                        rs.setAccountId(people.getId()+"");
                                        List<ConsumeRecord> consumeRecordListss=consumeRecordMapper.selectConsumeRecordList(rs);
                                        if(consumeRecordListss.size()>0){
                                        }else{
                                            if("4".equals(record.getPayMode())){
                                                ewmNum++;
                                                ewmMoney+=Double.parseDouble(record.getConsumeMoney());
                                            }else{
                                                overtime++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        //夜
                        //判断是否存在该时间段的记录，且补助过，有就过
                        //+补贴天数1天
                        if("3".equals(record.getTmrtype())) {
                            ConsumeRecord r=new ConsumeRecord();
                            r.setTmrtype("3");
                            r.setConsumeResult("0");
                            r.setStartTime(format.format(record.getStartDate())+" 21:00:00");
                            r.setEndTime(format.format(record.getStartDate())+" 23:59:59");
                            r.setAccountId(people.getId()+"");
                            List<ConsumeRecord> consumeRecordLists=consumeRecordMapper.selectConsumeRecordList(r);
                            //判断中餐有消费就不算晚餐，其他人员补贴次数一次
                            ConsumeRecord rs=new ConsumeRecord();
                            rs.setTmrtype("1");
                            rs.setConsumeResult("0");
                            rs.setStartTime(format.format(record.getStartDate())+" 10:00:00");
                            rs.setEndTime(format.format(record.getStartDate())+" 15:00:00");
                            rs.setAccountId(people.getId()+"");
                            List<ConsumeRecord> consumeRecordListss=consumeRecordMapper.selectConsumeRecordList(rs);
                            //晚餐
                            ConsumeRecord rrs=new ConsumeRecord();
                            rrs.setTmrtype("2");
                            rrs.setConsumeResult("0");
                            rrs.setStartTime(format.format(record.getStartDate())+" 15:00:00");
                            rrs.setEndTime(format.format(record.getStartDate())+" 21:00:00");
                            rrs.setAccountId(people.getId()+"");
                            List<ConsumeRecord> consumeRecordListsss=consumeRecordMapper.selectConsumeRecordList(rrs);
                            if("1".equals(people.getBranch().getXfbt())) {//总部人员补贴上限2次
                                if(consumeRecordListss.size()>0&&consumeRecordListsss.size()>0){
                                }else{
                                    if(consumeRecordLists.size()>0) {
                                        if(record.getId().equals(consumeRecordLists.get(0).getId())){
                                            if("4".equals(record.getPayMode())){
                                                ewmNum++;
                                                ewmMoney+=Double.parseDouble(record.getConsumeMoney());
                                            }else{
                                                subsidyDay++;
                                            }
                                        }
                                    }
                                }
                            }else{
                                if(consumeRecordListss.size()==0&&consumeRecordListsss.size()==0){
                                    if(consumeRecordLists.size()>0) {
                                        if(record.getId().equals(consumeRecordLists.get(0).getId())){
                                            if("4".equals(record.getPayMode())){
                                                ewmNum++;
                                                ewmMoney+=Double.parseDouble(record.getConsumeMoney());
                                            }else{
                                                subsidyDay++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                }
                ConsumeSubsidyRecord records=new ConsumeSubsidyRecord();
                records.setAccountId(people.getId());
                records.setEmpId(people.getGh());
                records.setAccountName(people.getName());
                records.setBranchId(people.getBranchId());
                records.setBranchName(people.getBranchName());
                records.setPosition(people.getPosition());
                records.setSubsidy(10L);
                records.setAllMoney((double) (10L*(subsidyDay+overtime+ewmNum)));
                records.setSubsidyDay(subsidyDay);
                records.setOvertime(overtime);
                records.setSubsidyDay(subsidyDay);
                records.setEwmMoney(ewmMoney);
                records.setEwmNum(ewmNum);
                try {
                    records.setStartDate(sdf.parse(lastDay+" 02:00:00"));
                    records.setEndDate(sdf.parse(last+" 02:00:00"));
                    records.setSubsidyTime(format.parse(last));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                consumeSubsidyRecordMapper.insertConsumeSubsidyRecord(records);
            }
        }
        return peopleList.size();
    }*/

    /**
     * 批量删除餐补明细
     *
     * @param ids 需要删除的餐补明细主键
     * @return 结果
     */
    @Override
    public int issueConsumeSubsidyRecordByIds(Long[] ids)
    {
        return consumeSubsidyRecordMapper.issueConsumeSubsidyRecordByIds(ids);
    }

    @Override
    public int examineConsumeSubsidyRecordByIds(Long[] ids) {
        return consumeSubsidyRecordMapper.examineConsumeSubsidyRecordByIds(ids);
    }

}
