package com.chinaweal.aiccs.aiccs.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSAttachment;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSBizRemListService;
import com.chinaweal.aiccs.aiccs.business.entity.TSTaskList;
import com.chinaweal.aiccs.aiccs.business.entity.dto.ResultMap;
import com.chinaweal.aiccs.aiccs.system.service.TRVerifyCommonService;
import com.chinaweal.aiccs.common.base.controller.BaseController;
import com.chinaweal.aiccs.common.constant.Constant;
import com.chinaweal.aiccs.common.util.ConstantsUtil;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
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.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.chinaweal.aiccs.aiccs.system.entity.TRVerifyCommon;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author lwd
 * @since 2021/06/28 21:05
 **/
@RestController
@Transactional
@RequestMapping("/verifyCommon")
public class TrverifyCommonController extends BaseController {
    @Autowired
    private TRVerifyCommonService trVerifyCommonService;

    /**
     * 用户常用语
     */
    @RequestMapping(value = "/verifyCommonList", method = RequestMethod.POST)
    @ResponseBody
    public RestResult<?> verifyCommonList(int rows, int page,String aicUserId) {

        if(aicUserId==null) throw new BusinessException("登录超时，请重新登录");
        IPage<TRVerifyCommon> TRVerifyCommonList = trVerifyCommonService.searchTRVerifyCommonByPage(new Page<>(page, rows), aicUserId);
        return RestResult.ok(TRVerifyCommonList);
    }

    /**
     * 获取用户常用语清单，不分页
     * @param userId
     * @return
     */
    @PostMapping("getAllCommonList")
    public RestResult<?> getAllCommonList(String userId) {
        AssertUtils.isNotBlank(userId);
        LambdaQueryWrapper<TRVerifyCommon> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TRVerifyCommon::getUserid,userId)
                .eq(TRVerifyCommon::getDstate, Constant.FALSE_0_STR)
                .orderByDesc(TRVerifyCommon::getLauptime);
        List<TRVerifyCommon> list = trVerifyCommonService.list(wrapper);
        return RestResult.ok(list);
    }

    /**
     * 按业务模块获取用户常用语清单，不分页
     * @param userId
     * @return
     */
    @PostMapping("getCommonListWithModule")
    public RestResult<?> getCommonListWithModule(String userId,String module) {
        AssertUtils.isNotBlank(userId);
        // 查找这个用户业务模块下的常用语
        List<TRVerifyCommon> list = trVerifyCommonService.selectWithModule(userId,module);
        // 查找这个用户没有模块区分的常用语
        List<TRVerifyCommon> trVerifyCommons = trVerifyCommonService.selectWithNoModule(userId, module);
        if(CollectionUtils.isNotEmpty(trVerifyCommons)){
            list.addAll(trVerifyCommons);
        }
        return RestResult.ok(list);
    }


    @PostMapping("/saveTRVerifyCommon")
    public RestResult<?> saveTRVerifyCommon(TRVerifyCommon trVerifyCommon) {

        if(trVerifyCommon.getCommonlanguage() != null){
            trVerifyCommon.setVerifycommonid(UUID.randomUUID().toString());
            trVerifyCommon.setUserid(trVerifyCommon.getUserid());
            trVerifyCommon.setDstate("0");
            trVerifyCommon.setCreatetime(LocalDate.now());
            trVerifyCommon.setLauptime(LocalDate.now());
            trVerifyCommonService.save(trVerifyCommon);
            return RestResult.ok(trVerifyCommon);
        }else {
            throw new BusinessException("添加失败，请重新试或联系管理人员");
        }

    }

    @PostMapping("/updateTRVerifyCommon")
    public RestResult<?> updateTRVerifyCommon(TRVerifyCommon trVerifyCommon) {

        if(StringUtils.isNoneBlank(trVerifyCommon.getVerifycommonid(),trVerifyCommon.getCommonlanguage())){
            LambdaQueryWrapper<TRVerifyCommon> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TRVerifyCommon::getVerifycommonid,trVerifyCommon.getVerifycommonid());
            TRVerifyCommon verifyCommon = trVerifyCommonService.getOne(wrapper);
            if(verifyCommon == null){
                return RestResult.error(ResultCode.DATA_NONE);
            }
            trVerifyCommon.setDstate("0");
            trVerifyCommon.setLauptime(LocalDate.now());
            trVerifyCommonService.updateById(trVerifyCommon);
            return RestResult.ok();
        }else {
            throw new BusinessException("添加失败，请重新试或联系管理人员");
        }

    }

    @PostMapping("/deleteTRVerifyCommon")
    public RestResult<?> deleteTRVerifyCommon(String commonId) {


        if(commonId != null){
            String[] split = commonId.split(",");
            List<String> commonIdList = Arrays.asList(split);
            for (int i = 0; i < commonIdList.size(); i++) {
                trVerifyCommonService.deletaBycommonId(commonIdList.get(i));
            }
            return RestResult.ok(commonId);
        }else {
            throw new BusinessException("添加失败，请重新试或联系管理人员");
        }

    }

}
