package com.chinaweal.aiccs.aiccs.punishment.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.punishment.entity.Punishmeasures;
import com.chinaweal.aiccs.aiccs.punishment.job.Task;
import com.chinaweal.aiccs.aiccs.punishment.service.PunishmeasuresService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.springboot.util.AssertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.*;


/**
 * <p>
 *  惩戒措施前端控制器
 * </p>
 *
 * @author Lee
 * @since 2021-06-30
 */
@RestController
@RequestMapping("/punishmeasures")
@Transactional
public class PunishmeasuresController extends BaseController {

    @Autowired
    private PunishmeasuresService punishmeasuresService;

    /**
     * 获取惩戒措施列表
     * @return
     */
    @GetMapping("getMeasureList")
    //public RestResult<?> getMeasureList(String departmentCode){
    public RestResult<?> getMeasureList(String punishfrom){
        LambdaQueryWrapper<Punishmeasures> wrapper = new LambdaQueryWrapper<>();
        if(("null").equals(punishfrom)||punishfrom.equals("")){
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        String departmentCode= Task.PunishFromAndDepartmentMap.get(punishfrom);
        wrapper.eq(Punishmeasures::getDState,"0");
        wrapper.eq(Punishmeasures::getDepartmentCode,departmentCode);
        List<Punishmeasures> list = punishmeasuresService.list(wrapper);
        return RestResult.ok(list);
    }


    /**
     * 获取惩戒措施列表分页
     * @param pageRequestDto 分页参数
     * @return
     */
    @PostMapping("measureListPage")
    public RestResult<?> measureListPage(@RequestBody PageRequestDto pageRequestDto) {
        Map<String, Object> customParamMap = pageRequestDto.getCustomParamMap();
        String searchMeasure = "";
        String searchDepart = "";

        if(customParamMap != null){
            searchMeasure = StringUtils.tranObject(customParamMap.get("searchMeasure"));
            searchDepart = StringUtils.tranObject(customParamMap.get("searchDepart"));
        }
        LambdaQueryWrapper<Punishmeasures> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(searchMeasure),Punishmeasures::getMeasure,searchMeasure)
                .eq(Punishmeasures::getDState,"0")
                .eq(StringUtils.isNotBlank(searchDepart),Punishmeasures::getDepartment,searchDepart);
        Page<Punishmeasures> page = punishmeasuresService.page(new Page<>(pageRequestDto.getCurPage(), pageRequestDto.getPageSize()), wrapper);
        return RestResult.ok(page);
    }

    /**
     * 根据措施id删除，逻辑删
     * @param measureIds
     * @return
     */
    @PostMapping("deleteMeasure")
    public RestResult<?> deleteMeasure(String measureIds, HttpServletRequest request) {
        AICUser curUser = getLoginUser(request);
        if(StringUtils.isBlank(measureIds)){
            return RestResult.error(ResultCode.PARAM_IS_BLANK);
        }
        String[] ids = StringUtils.split(measureIds, Constant.SEMICOLON);
        LambdaQueryWrapper<Punishmeasures> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Punishmeasures::getMeasureId, Arrays.asList(ids))
                .eq(Punishmeasures::getDState,"0");
        List<Punishmeasures> list = punishmeasuresService.list(wrapper);
        if(CollectionUtils.isEmpty(list)){
            return RestResult.error(ResultCode.DATA_NONE);
        }
        boolean flag = false;
        for (Punishmeasures punishmeasures : list) {
            LocalDateTime now = LocalDateTime.now();
            punishmeasures.setDState("1");
            punishmeasures.setUpdateTime(now);
            punishmeasures.setUpdateUserId(curUser.getPrimaryKey());
            punishmeasures.setLaupTime(now);
            flag= punishmeasuresService.updateById(punishmeasures);
        }

        return RestResult.ok(flag);

    }

    /**
     * 保存措施
     * @param punishmeasure
     * @return
     */
    @PostMapping("saveMeasure")
    public RestResult<?> saveMeasure(Punishmeasures punishmeasure, HttpServletRequest request) {

        AICUser curUser = getLoginUser(request);
        if (StringUtils.isNotBlank(punishmeasure.getMeasureId())){// 有id 为编辑操作
            punishmeasure.setUpdateTime(LocalDateTime.now());
            punishmeasure.setUpdateUserId(curUser.getPrimaryKey());
        }else { // 新增操作
            punishmeasure.setDState("0");
            punishmeasure.setCreateTime(LocalDateTime.now());
            punishmeasure.setCreateUserId(curUser.getPrimaryKey());
        }
        punishmeasure.setLaupTime(LocalDateTime.now());
        return RestResult.ok(punishmeasuresService.saveOrUpdate(punishmeasure));
    }

    @GetMapping("/queryAll")
    public RestResult<?> queryAll(){
        LambdaQueryWrapper<Punishmeasures> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Punishmeasures::getDState,"0");
        List<Punishmeasures> punishmeasures = punishmeasuresService.list(wrapper);
        return RestResult.ok(punishmeasures);
    }



}

