package com.xjrsoft.module.evaluate.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.evaluate.dto.AddEvaluateManageDto;
import com.xjrsoft.module.evaluate.dto.EvaluateManageChangeStatusDto;
import com.xjrsoft.module.evaluate.dto.EvaluateManagePageDto;
import com.xjrsoft.module.evaluate.dto.UpdateEvaluateManageDto;
import com.xjrsoft.module.evaluate.entity.EvaluateManage;
import com.xjrsoft.module.evaluate.entity.EvaluateTemplate;
import com.xjrsoft.module.evaluate.service.IEvaluateManageService;
import com.xjrsoft.module.evaluate.service.IEvaluateTemplateService;
import com.xjrsoft.module.evaluate.vo.EvaluateManagePageVo;
import com.xjrsoft.module.evaluate.vo.EvaluateManageVo;
import com.xjrsoft.module.system.entity.DictionaryDetail;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
* @title: 评价管理
* @Author dzx
* @Date: 2024-01-16
* @Version 1.0
*/
@RestController
@RequestMapping("/evaluate" + "/evaluateManage")
@Api(value = "/evaluate"  + "/evaluateManage",tags = "评价管理代码")
@AllArgsConstructor
public class EvaluateManageController {


    private final IEvaluateManageService evaluateManageService;
    private final IEvaluateTemplateService evaluateTemplateService;

    @GetMapping(value = "/page")
    @ApiOperation(value="评价管理列表(分页)")
    @SaCheckPermission("evaluatemanage:detail")
    public RT<PageOutput<EvaluateManagePageVo>> page(@Valid EvaluateManagePageDto dto){
        Page<EvaluateManagePageVo> page = evaluateManageService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<EvaluateManagePageVo> pageOutput = ConventPage.getPageOutput(page, EvaluateManagePageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询评价管理信息")
    @SaCheckPermission("evaluatemanage:detail")
    public RT<EvaluateManageVo> info(@RequestParam Long id){
        MPJLambdaWrapper<EvaluateManage> evaluateManageMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateManageMPJLambdaWrapper
                .select(EvaluateManage::getId)
                .selectAs(EvaluateManage::getSortCode, EvaluateManageVo::getSortCode)
                .selectAs(EvaluateTemplate::getEvaluateType, EvaluateManageVo::getEvaluateType)
                .select(EvaluateManage.class, x -> VoToColumnUtil.fieldsToColumns(EvaluateManageVo.class).contains(x.getProperty()))
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, EvaluateManage::getCreateFrequency,ext->ext
                        .selectAs(DictionaryDetail::getName,EvaluateManageVo::getCreateFrequencyCn))
                .leftJoin(EvaluateTemplate.class, EvaluateTemplate::getId, EvaluateManage::getEvaluateTemplateId)
                .leftJoin(DictionaryDetail.class, DictionaryDetail::getCode, EvaluateTemplate::getEvaluateType,ext->ext
                        .selectAs(DictionaryDetail::getName,EvaluateManageVo::getEvaluateTypeCn))
                .eq(EvaluateManage::getId, id)
                .disableSubLogicDel();
        EvaluateManageVo evaluateManageVo = evaluateManageService.selectJoinOne(EvaluateManageVo.class, evaluateManageMPJLambdaWrapper);
        if (evaluateManageVo == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(evaluateManageVo);
    }

    @PostMapping(value = "/change-status")
    @ApiOperation(value="修改状态")
    @SaCheckPermission("evaluatemanage:edit")
    public RT<Boolean> changeStatus(@Valid @RequestBody EvaluateManageChangeStatusDto dto){
        if(dto.getStatus() == 1){
            Boolean release = evaluateManageService.release(dto.getId());
            return RT.ok(release);
        }
        if(dto.getStatus() == 0){
            EvaluateManage evaluateManage = BeanUtil.toBean(dto, EvaluateManage.class);
            evaluateManage.setModifyDate(new Date());
            evaluateManage.setModifyUserId(StpUtil.getLoginIdAsLong());

            return RT.ok(evaluateManageService.updateById(evaluateManage));
        }
        if(dto.getStatus() == 2){
            return RT.ok(evaluateManageService.updateById(new EvaluateManage(){{
                setId(dto.getId());
                setCreateDate(new Date());
                setCreateUserId(StpUtil.getLoginIdAsLong());
                setStatus(1);
            }}));
        }

        return RT.ok(true);
    }


    @PostMapping
    @ApiOperation(value = "新增评价管理")
    @SaCheckPermission("evaluatemanage:add")
    public RT<Boolean> add(@Valid @RequestBody AddEvaluateManageDto dto){
        EvaluateManage evaluateManage = BeanUtil.toBean(dto, EvaluateManage.class);
        evaluateManage.setCreateDate(new Date());
        evaluateManage.setCreateUserId(StpUtil.getLoginIdAsLong());
        //获取sortCode的最大数量
        QueryWrapper<EvaluateManage> queryWrapperSortcode = new QueryWrapper<>();
        queryWrapperSortcode.select("IFNULL(MAX(sort_code),0) as sortCode");
        EvaluateManage e = evaluateManageService.getOne(queryWrapperSortcode);
        int sortCode = e.getSortCode() + 1;
        evaluateManage.setSortCode(sortCode);

        boolean isSuccess = evaluateManageService.add(evaluateManage);
        return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改评价管理")
    @SaCheckPermission("evaluatemanage:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateEvaluateManageDto dto){

        EvaluateManage evaluateManage = BeanUtil.toBean(dto, EvaluateManage.class);
        return RT.ok(evaluateManageService.update(evaluateManage));

    }

    @DeleteMapping
    @ApiOperation(value = "删除评价管理")
    @SaCheckPermission("evaluatemanage:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(evaluateManageService.delete(ids));
    }

}