package org.jeecg.modules.projectManagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.modules.projectManagement.entity.FdWorkingHours;
import org.jeecg.modules.projectManagement.entity.FdWorkingHoursItems;
import org.jeecg.modules.projectManagement.mapper.FdWorkingHoursMapper;
import org.jeecg.modules.projectManagement.service.IFdWorkingHoursItemsService;
import org.jeecg.modules.projectManagement.service.IFdWorkingHoursService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 工时记录
 * @author： jeecg-boot
 * @date： 2019-06-25
 * @version： V1.0
 */

@Service
@Transactional
public class FdWorkingHoursServiceImpl extends ServiceImpl<FdWorkingHoursMapper, FdWorkingHours> implements IFdWorkingHoursService {

    @Resource
    FdWorkingHoursMapper fdWorkingHoursMapper;

    @Resource
    IFdWorkingHoursService fdWorkingHoursService;

    @Resource
    IFdWorkingHoursItemsService fdWorkingHoursItemsService;

    @Override
    public IPage<FdWorkingHours> findByPage(Page<FdWorkingHours> page, QueryWrapper<FdWorkingHours> queryWrapper) {
        return fdWorkingHoursMapper.findByPage(page, queryWrapper);
    }

    /**
     * @return void
     * @Author gurui
     * @Description //TODO 添加工时记录同时添加工时记录明细
     * @Date 14:59 2019/6/25
     * @Param [fdWorkingHours]
     **/
    @Override
    public void addAll(FdWorkingHours fdWorkingHours) {
        // if (fdWorkingHours.getId() == null || fdWorkingHours.getId().equals("")){
        //     fdWorkingHours.setDelFlag("0");
        //     fdWorkingHoursService.save(fdWorkingHours);
        // }else{
        //     LambdaQueryWrapper<FdWorkingHoursItems> queryWrapper = new LambdaQueryWrapper<>();
        //     queryWrapper.eq(FdWorkingHoursItems::getId,fdWorkingHours.getPkWorkHoursItems());
        //     fdWorkingHoursItemsService.remove(queryWrapper);
        //     fdWorkingHoursService.updateById(fdWorkingHours);
        // }
        // if (fdWorkingHours.getFdWorkingHoursItemsList()!=null){
        //     for (FdWorkingHoursItems fdWorkingHoursItems:fdWorkingHours.getFdWorkingHoursItemsList()){
        //         fdWorkingHoursItems.setId(fdWorkingHours.getPkWorkHoursItems());
        //     }
        //     fdWorkingHoursItemsService.saveBatch(fdWorkingHours.getFdWorkingHoursItemsList());
        // }
        SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
        // fdWorkingHours.setHourFillerId(user.getPersonnelId());
        fdWorkingHours.setDelFlag("0");
        //获取项目总工时与非项目总工时
        getHoursTotal(fdWorkingHours);
        fdWorkingHoursService.save(fdWorkingHours);

        if (fdWorkingHours.getFdWorkingHoursItemsList()!=null&&fdWorkingHours.getFdWorkingHoursItemsList().size()>0) {
            int i = 1;
            for (FdWorkingHoursItems fdWorkingHoursItems : fdWorkingHours.getFdWorkingHoursItemsList()) {
                fdWorkingHoursItems.setPkWokingHour(fdWorkingHours.getId());
                fdWorkingHoursItems.setSort(i + "");
                i++;
            }
            fdWorkingHoursItemsService.saveBatch(fdWorkingHours.getFdWorkingHoursItemsList());
        }
    }

    /**
    * @Author chenchuang
    * @Description //TODO 获取项目总工时与非项目总工时
    * @Date 2020/6/22 15:52
    * @Param [fdWorkingHours]
    * @return void
    */
    public void getHoursTotal(FdWorkingHours fdWorkingHours){
        if (fdWorkingHours.getFdWorkingHoursItemsList()!=null&&fdWorkingHours.getFdWorkingHoursItemsList().size()>0) {
            Double projectHoursTotal=0.0;//项目总工时
            Double nonPorjectHoursTotal=0.0;//非项目总工时
            //循环子表
            for(FdWorkingHoursItems fdWorkingHoursItems:fdWorkingHours.getFdWorkingHoursItemsList()){
                String workingHoursNumber=fdWorkingHoursItems.getWorkingHoursNumber();//工时
                //判断工时是否为空是否为数字或者小数
                if(StringUtils.isNotBlank(workingHoursNumber)&&isNumber(workingHoursNumber)){
                    //统计项目工时总计
                    if(fdWorkingHoursItems.getSpecialType().equals("1")){
                        projectHoursTotal+=Double.parseDouble(workingHoursNumber);
                    }
                    //统计非项目工时总计
                    if(fdWorkingHoursItems.getSpecialType().equals("2")){
                        nonPorjectHoursTotal+=Double.parseDouble(workingHoursNumber);
                    }
                }
            }
            fdWorkingHours.setProjectHoursTotal(projectHoursTotal+"");
            fdWorkingHours.setNonProjectHoursTotal(nonPorjectHoursTotal+"");
        }
    }

    public static boolean isNumber(String str){
        String reg = "^[0-9]+(.[0-9]+)?$";
        return str.matches(reg);
    }

    /***
     * @Title:删除工时记录同时删除相关记录项
     * @Description:TODO
     * @param id
     * @return boolean
     * @exception
     * @author 陈创
     * @date 2019/6/26 18:30
     * @updatehistory:
     *   2019/6/26 18:30 新增
     */
    @Override
    public boolean deleteAll(String id) {
        boolean result = true;
        //删除工时记录
        try {
            fdWorkingHoursService.removeById(id);
            LambdaQueryWrapper<FdWorkingHoursItems> workingHoursItemsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            workingHoursItemsLambdaQueryWrapper.eq(FdWorkingHoursItems::getPkWokingHour, id);
            fdWorkingHoursItemsService.remove(workingHoursItemsLambdaQueryWrapper);
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO 弃审核数据 其状态变为0
     * @Date 17:26 2019/7/9
     * @Param [ids]
     **/
    @Override
    public Map<String, Object> auditBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unAudit = " ";//可审计
        String audit = " ";//已审计
        //根据多个id查询报价对象集合
        List<FdWorkingHours> fdWorkingHoursList = fdWorkingHoursMapper.selectBatchIds(ids);
        //修改
        if (fdWorkingHoursList == null || fdWorkingHoursList.isEmpty()) {
            audit += "未找到修改数据</br>";
        } else {
            //判断哪些可审计哪些不可审计
            for (FdWorkingHours fdWorkingHours : fdWorkingHoursList) {
                if (fdWorkingHours.getStatus().equals("0")) {
                    audit += fdWorkingHours.getCode() + "，";
                } else {
                    unAudit += fdWorkingHours.getCode() + "，";
                }
            }
            //可审计存在则操作数据库修改
            if (!unAudit.equals("")) {
                fdWorkingHoursMapper.updateAuditBatch(ids);
            }
        }
        //去掉最后一个","
        audit = audit.substring(0, audit.length() - 1);
        unAudit = unAudit.substring(0, unAudit.length() - 1);
        //加关键词
        if (!unAudit.equals("")) {
            unAudit = unAudit + "弃审核成功";
        }
        if (!audit.equals("")) {
            audit = "其中:" + audit + "已处于未审核状态。<br> ";
        }
        map.put("msg", unAudit + audit);
        return map;
    }

    /**
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Author gurui
     * @Description //TODO  审核数据 其状态变为1
     * @Date 17:26 2019/7/9
     * @Param [ids]
     **/
    @Override
    public Map<String, Object> reviewBatch(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        String unReview = " "; //未审核
        String review = " "; //已审核
        //根据多个id查询报价对象集合
        List<FdWorkingHours> fdWorkingHoursList = fdWorkingHoursMapper.selectBatchIds(ids);
        //修改
        if (fdWorkingHoursList == null || fdWorkingHoursList.isEmpty()) {
            review += "未找到修改数据</br>";
        } else {
            //判断哪些已审核哪些未审核
            for (FdWorkingHours fdWorkingHours : fdWorkingHoursList) {
                if (fdWorkingHours.getStatus().equals("0")) {
                    unReview += fdWorkingHours.getCode() + "，";
                }
                if (fdWorkingHours.getStatus().equals("1")) {
                    review += fdWorkingHours.getCode() + "，";
                }
                if (fdWorkingHours.getStatus().equals("2")) {
                    throw new RuntimeException("已关闭！无法审核");
                }
                if (fdWorkingHours.getStatus().equals("3")) {
                    throw new RuntimeException("已转合同！无法审核");
                }
            }
            //去掉最后一个","号
            unReview = unReview.substring(0, unReview.length() - 1);
            review = review.substring(0, review.length() - 1);
            //操作数据库将未审核的修改成已审核
            if (!unReview.equals("")) {
                fdWorkingHoursMapper.updateReviewBatch(ids);
            }
        }
        //加关键词
        if (!unReview.equals("")) {
            unReview = unReview + "审核成功";
        }
        if (!review.equals("")) {
            review = "其中：" + review + "已审核。<br>";
        }
        map.put("msg", unReview + review);
        return map;
    }

    /***
     * @Title:查询当天是否做过工时
            * @Description:TODO 根据当天时间，登陆人，查询工时数据是否存在
            * @param nowDate, createBy]
            * @return int
            * @exception
            * @author 陈创
            * @date 2019/9/10 10:55
            * @updatehistory:
     *   2019/9/10 10:55 新增
            */
    @Override
    public int getNowDayCount(String nowDate, String createBy) {
        int count=fdWorkingHoursMapper.getNowDayCount(nowDate,createBy);
        return count;
    }

    @Override
    public IPage<FdWorkingHours> examinePage(Page<FdWorkingHours> page, QueryWrapper<FdWorkingHours> queryWrapper) {
        return fdWorkingHoursMapper.examinePage(page, queryWrapper);
    }

    @Override
    public void updateStatus(FdWorkingHours fdWorkingHours) {
        fdWorkingHoursMapper.updateStatus(fdWorkingHours);
    }

    @Override
    public void updateHoursTotal(FdWorkingHours fdWorkingHours) {
        //根据修改的工时id查询对应子表
        LambdaQueryWrapper<FdWorkingHoursItems> fdWorkingHoursItemsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        fdWorkingHoursItemsLambdaQueryWrapper.eq(FdWorkingHoursItems::getDelFlag,"0");
        fdWorkingHoursItemsLambdaQueryWrapper.eq(FdWorkingHoursItems::getPkWokingHour,fdWorkingHours.getId());
        List<FdWorkingHoursItems> fdWorkingHoursItemsList=fdWorkingHoursItemsService.list(fdWorkingHoursItemsLambdaQueryWrapper);
        fdWorkingHours.setFdWorkingHoursItemsList(fdWorkingHoursItemsList);
        //计算总工时
        getHoursTotal(fdWorkingHours);
        fdWorkingHoursService.updateById(fdWorkingHours);
    }


}
