package com.nantian.cloud.jljc.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nantian.cloud.jljc.entity.TFaAccreditingBody;
import com.nantian.cloud.jljc.entity.TFaFixedAssets;
import com.nantian.cloud.jljc.service.TFaAccreditingBodyService;
import com.nantian.cloud.jljc.service.TFaFixedAssetsService;
import com.nantian.cloud.jljc.vo.TFaTimeVo;
import com.nantian.sc.cloud.common.core.util.R;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.log.annotation.SysLog;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 固定资产投资项目节能审查评审机构负责人表 前端控制器
 * </p>
 *
 * @author fyf
 * @since 2022-02-15
 */
@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("/tFaAccreditingBody")
public class TFaAccreditingBodyController {

    @Autowired
    TFaAccreditingBodyService tFaAccreditingBodyService;

    @Autowired
    TFaFixedAssetsService tFaFixedAssetsService;

    @PostMapping("/save")
    @SysLog("新增固定资产投资项目节能审查评审机构负责人表")
    public R save(@RequestBody TFaAccreditingBody tFaAccreditingBody){
        tFaAccreditingBodyService.save(tFaAccreditingBody);
        return R.ok();
    }

    /**
     * 报送评审机构
     * @param id
     * @param limit
     * @return
     */
    @GetMapping("/submission")
    @SysLog("报送评审机构")
    public R submission(@Param("id") Integer id, @Param("limit") Integer limit){
        ScUser u = SecurityUtils.getUser();
        TFaAccreditingBody tFaAccreditingBody = new TFaAccreditingBody();
        tFaAccreditingBody.setUpdateTime(new Date());
        tFaAccreditingBody.setDeptId(u.getDeptId());
        tFaAccreditingBody.setUserId(u.getId());
        tFaAccreditingBody.setProjectId(id);
        tFaAccreditingBody.setTimeLimit(limit);
        tFaAccreditingBody.setActionTime(new Date());
        tFaAccreditingBody.setDelFlag(1);
        tFaAccreditingBodyService.save(tFaAccreditingBody);

        TFaFixedAssets tFaFixedAssets = tFaFixedAssetsService.getById(id);
        tFaFixedAssets.setStatusData(1);
        tFaFixedAssets.setProjectStatus(2);
        tFaFixedAssets.setUserId(u.getId());
        tFaFixedAssets.setDeptId(u.getDeptId());
        tFaFixedAssets.setUpdateTime(new Date());
        tFaFixedAssets.setUsername(u.getUsername());
        tFaFixedAssetsService.updateById(tFaFixedAssets);
        return R.ok();
    }

    /**
     * 更新负责人、专家、职能部门人员信息
     * @param tFaAccreditingBody
     * @return
     */
    @PutMapping("/putAccInfo")
    @SysLog("更新负责人、专家、职能部门人员信息")
    public R putAccInfo(@RequestBody TFaAccreditingBody tFaAccreditingBody){
        ScUser u = SecurityUtils.getUser();
        tFaAccreditingBody.setUserId(u.getId());
        tFaAccreditingBody.setDeptId(u.getDeptId());
        tFaAccreditingBody.setUpdateTime(new Date());
        tFaAccreditingBodyService.updateById(tFaAccreditingBody);
        return R.ok();
    }

    @GetMapping("/info/{id}")
    @SysLog("根据id获取固定资产投资项目节能审查评审机构负责人表")
    public R getInfo(@PathVariable Integer id){
        ScUser u = SecurityUtils.getUser();
        TFaAccreditingBody faAccreditingBody = tFaAccreditingBodyService.getById(id);
        Boolean auth = new Boolean(false);
        if (u.getDeptId().equals(530000)){
            auth = true;
        }else{
            auth = u.getUsername().equals(faAccreditingBody.getPersonName());
        }
        return R.ok(MapUtil.builder().put("faAccreditingBody", faAccreditingBody)
                .put("auth",auth).put("isSure",u.getDeptId().equals(530000)).map());
    }

    /**
     * 任务时限
     */
    @GetMapping("/time")
    @SysLog("任务时限")
    public R timeOut() throws ParseException {
        //根据数据库数据条数生成任务
        List<TFaAccreditingBody> list = tFaAccreditingBodyService.getProjectTime(new DataScope());
        List<TFaTimeVo> timeList = new ArrayList<>();
        for (TFaAccreditingBody tFaAccreditingBody : list) {
            TFaFixedAssets tFaFixedAssets = tFaFixedAssetsService.getOne(
                    new QueryWrapper<TFaFixedAssets>()
                            .eq("del_flag", 1)
                            .eq("project_status",2)
                            .eq("id", tFaAccreditingBody.getProjectId()));
            if (tFaFixedAssets != null){
                TFaTimeVo tFaTimeVo = new TFaTimeVo();
                Integer timeLimit = tFaAccreditingBody.getTimeLimit();
                Date actionTime = tFaAccreditingBody.getActionTime();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(actionTime);
//                int day = calendar.get(Calendar.DATE);
                calendar.add(Calendar.DAY_OF_MONTH, timeLimit);
                Date endTime = calendar.getTime();
                Date date = new Date();
                long l = (endTime.getTime() - date.getTime() + 1000000) / (3600 * 24 * 1000);
                if (tFaAccreditingBody.getSuspendTime() != null){
                    if (tFaAccreditingBody.getRestartTime() != null){

                        Calendar c1 = Calendar.getInstance();
                        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
                        //暂停时间
                        Date parse = dateFormat.parse(dateFormat.format(tFaAccreditingBody.getSuspendTime()));
                        c1.setTime(parse);
                        long date1 =c1.getTimeInMillis();
                        //开始时间
                        Date parse1 = dateFormat.parse(dateFormat.format(tFaAccreditingBody.getActionTime()));
                        c1.setTime(parse1);
                        long date2 = c1.getTimeInMillis();
                        //过去几天
                        long t = (date1 - date2 + 1000000) / (3600 * 24 * 1000) + 1;
                        //剩余几天
                        long time = timeLimit - t;
                        //结束时间
                        Calendar cal = new GregorianCalendar();
                        cal.setTime(tFaAccreditingBody.getRestartTime());
                        cal.add(Calendar.DAY_OF_MONTH, (int) time);

                        //获取当前时间
                        Date date3 = new Date();
                        c1.setTime(date3);
                        long timeInMillis = c1.getTimeInMillis();
                        Date time1 = cal.getTime();
                        c1.setTime(time1);
                        long timeInMillis1 = c1.getTimeInMillis();
                        //重启后的剩余天数
                        long l1 = (timeInMillis1 - timeInMillis + 1000000) / (3600 * 24 * 1000);
                        tFaTimeVo.setLimit(timeLimit);
                        tFaTimeVo.setActionTime(actionTime);
                        tFaTimeVo.setPrincipal(tFaAccreditingBody.getPersonName());
                        tFaTimeVo.setProjectId(tFaFixedAssets.getId());
                        tFaTimeVo.setProjectName(tFaFixedAssets.getProjectName());
                        tFaTimeVo.setSuspendTime(tFaAccreditingBody.getSuspendTime());
                        tFaTimeVo.setRestartTime(tFaAccreditingBody.getRestartTime());
                        tFaTimeVo.setEndTime(cal.getTime());
                        tFaTimeVo.setMark(3);
                        tFaTimeVo.setDays(l1);
                        timeList.add(tFaTimeVo);
                    }else{
                        long i = (endTime.getTime() - tFaAccreditingBody.getSuspendTime().getTime() + 1000000) / (3600 * 24 * 1000);
                        tFaTimeVo.setLimit(timeLimit);
                        tFaTimeVo.setActionTime(actionTime);
                        tFaTimeVo.setPrincipal(tFaAccreditingBody.getPersonName());
                        tFaTimeVo.setProjectId(tFaFixedAssets.getId());
                        tFaTimeVo.setProjectName(tFaFixedAssets.getProjectName());
                        tFaTimeVo.setSuspendTime(tFaAccreditingBody.getSuspendTime());
                        Date date1 = new Date(2099-1900,12-1,31);
                        tFaTimeVo.setEndTime(date1);
                        tFaTimeVo.setRestartTime(null);
                        tFaTimeVo.setMark(2);
                        tFaTimeVo.setDays(i);
                        timeList.add(tFaTimeVo);
                    }
                }else {
                    tFaTimeVo.setLimit(timeLimit);
                    tFaTimeVo.setActionTime(actionTime);
                    tFaTimeVo.setPrincipal(tFaAccreditingBody.getPersonName());
                    tFaTimeVo.setProjectId(tFaFixedAssets.getId());
                    tFaTimeVo.setProjectName(tFaFixedAssets.getProjectName());
                    tFaTimeVo.setMark(1);
                    tFaTimeVo.setSuspendTime(null);
                    tFaTimeVo.setRestartTime(null);
                    tFaTimeVo.setEndTime(endTime);
                    tFaTimeVo.setDays(l);
                    timeList.add(tFaTimeVo);
                }
            }
        }
        if (timeList.size() > 0) { //结果集大于0，进行排序
            Collections.sort(timeList, new Comparator<TFaTimeVo>() {
                @Override
                public int compare(TFaTimeVo o1, TFaTimeVo o2) {
                    return o1.getEndTime().compareTo(o2.getEndTime());
                }
            });
            if (timeList.size() > 3){
                timeList = timeList.subList(0,3);
            }else {
                timeList = timeList.subList(0,timeList.size());
            }
        }
        return R.ok(timeList);
    }

    @GetMapping("/suspend/{id}")
    public R suspend(@PathVariable Integer id){
        ScUser u = SecurityUtils.getUser();
        TFaAccreditingBody tFaAccreditingBody = tFaAccreditingBodyService.getOne(
                new QueryWrapper<TFaAccreditingBody>()
                        .eq("del_flag",1)
                        .eq("project_id", id));
        tFaAccreditingBody.setSuspendTime(new Date());
        tFaAccreditingBody.setDeptId(u.getDeptId());
        tFaAccreditingBody.setUpdateTime(new Date());
        tFaAccreditingBody.setUserId(u.getId());
        tFaAccreditingBodyService.updateById(tFaAccreditingBody);
        return R.ok();
    }

    @GetMapping("/action/{id}")
    public R action(@PathVariable Integer id){
        ScUser u = SecurityUtils.getUser();
        TFaAccreditingBody tFaAccreditingBody = tFaAccreditingBodyService.getOne(
                new QueryWrapper<TFaAccreditingBody>()
                        .eq("del_flag",1)
                        .eq("project_id", id));
        tFaAccreditingBody.setRestartTime(new Date());
        tFaAccreditingBody.setDeptId(u.getDeptId());
        tFaAccreditingBody.setUpdateTime(new Date());
        tFaAccreditingBody.setUserId(u.getId());
        tFaAccreditingBodyService.updateById(tFaAccreditingBody);
        return R.ok();
    }

}
