package com.hzs.system.config.controller.manage;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.system.config.BdPurpose;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.service.IBdPurposeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 领用用途表 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2024-04-22
 */
@RestController
@RequestMapping("/manage/bd-purpose")
public class BdPurposeController extends BaseController {

    @Autowired
    private IBdPurposeService iBdPurposeService;

    /**
     * @description: 领用用途配置列表
     * @author: zhang jing
     * @date: 2024/4/22 10:51
     * @param: [purpose]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdPurpose purpose) {
        startPage();
        QueryWrapper<BdPurpose> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(purpose.getPurpose())) {
            queryWrapper.like("PURPOSE", purpose.getPurpose());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdPurpose> list = iBdPurposeService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * @description: 用途下拉选列表
     * @author: zhang jing
     * @date: 2024/4/23 14:25
     * @param: [purpose]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/allList")
    public TableDataInfo allList(BdPurpose purpose) {
        QueryWrapper<BdPurpose> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(purpose.getPurpose())) {
            queryWrapper.like("PURPOSE", purpose.getPurpose());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdPurpose> list = iBdPurposeService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * @description: 导出领用用途配置
     * @author: zhang jing
     * @date: 2023/3/9 16:08
     * @param: [response, purpose]
     * @return: void
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.SELECT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BdPurpose purpose) {
        QueryWrapper<BdPurpose> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(purpose.getPurpose())) {
            queryWrapper.like("PURPOSE", purpose.getPurpose());
        }
        queryWrapper.orderByDesc("CREATION_TIME");
        List<BdPurpose> list = iBdPurposeService.list(queryWrapper);
        ExcelUtil<BdPurpose> util = new ExcelUtil<>(BdPurpose.class);
        util.exportExcel(response, list, "领用用途配置");
    }

    /**
     * @description: 新增领用用途配置
     * @author: zhang jing
     * @date: 2024/4/22 10:51
     * @param: [purpose]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdPurpose purpose) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(purpose.getPurpose())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_PURPOSE_NOT_NULL));
        }
        QueryWrapper<BdPurpose> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        if (StringUtils.isNotNull(purpose.getPurpose())) {
            queryWrapper.eq("PURPOSE", purpose.getPurpose());
        }
        List<BdPurpose> list = iBdPurposeService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_PURPOSE_REPEAT));
        }
        purpose.setCreationTime(new Date());
        purpose.setPkCreator(SecurityUtils.getUserId());
        purpose.setPkCountry(pkCountry);
        return AjaxResult.success(iBdPurposeService.save(purpose));
    }



    /**
     * @description:修改领用用途配置
     * @author: zhang jing
     * @date: 2024/4/22 10:51
     * @param: [purpose]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdPurpose purpose) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(purpose.getPurpose())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_PURPOSE_NOT_NULL));
        }
        QueryWrapper<BdPurpose> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        if (StringUtils.isNotNull(purpose.getPurpose())) {
            queryWrapper.eq("PURPOSE", purpose.getPurpose());
            queryWrapper.ne("PK_ID",purpose.getPkId());
        }
        List<BdPurpose> list = iBdPurposeService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.BD_PURPOSE_REPEAT));
        }
        purpose.setCreationTime(new Date());
        purpose.setPkCreator(SecurityUtils.getUserId());
        purpose.setPkCountry(pkCountry);
        return toAjax(iBdPurposeService.updateById(purpose));
    }

    /**
     * @description: 修改领用用途配置
     * @author: zhang jing
     * @date: 2024/4/22 10:51
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        BdPurpose purpose = iBdPurposeService.getById(pkId);
        return AjaxResult.success(purpose);
    }


    /**
     * @description: 删除领用用途配置
     * @author: zhang jing
     * @date: 2024/4/22 10:52
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.BDPURPOSE_CONFIG,business = EOperationBusiness.BDPURPOSE_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<BdPurpose> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(iBdPurposeService.update(updateWrapper));
    }

}
