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


import cn.hutool.core.bean.BeanUtil;
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.*;
import com.hzs.common.core.utils.StringUtils;
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.BdContinent;
import com.hzs.common.domain.system.ext.HoGlobalCompanyExt;
import com.hzs.common.domain.system.home.HoGlobalCompany;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.config.service.IBdContinentService;
import com.hzs.system.home.service.IHoGlobalCompanyService;
import com.hzs.system.home.vo.HoGlobalCompanyVO;
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 hzs
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/manage/ho-global-company")
public class HoGlobalCompanyController extends BaseController {

    @Autowired
    private IHoGlobalCompanyService globalCompanyService;
    @Autowired
    private IBdContinentService continentService;

    /**
     * @description: 全球分布
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [globalCompany]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.GLOBAL_DISTRIBUTION,business = EOperationBusiness.GLOBAL_DISTRIBUTION, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(HoGlobalCompanyVO globalCompanyVO) {
        startPage();
        List<HoGlobalCompanyVO> list = globalCompanyService.queryGlobalCompanyList(globalCompanyVO);
        return getDataTable(list);
    }


    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2022/12/8 17:04
     * @param: [language, response, globalCompanyExt]
     * @return: void
     **/
    @Log(module = EOperationModule.GLOBAL_DISTRIBUTION,business = EOperationBusiness.GLOBAL_DISTRIBUTION, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,HoGlobalCompanyVO globalCompanyVO) {
        List<HoGlobalCompanyVO> list = globalCompanyService.queryGlobalCompanyList(globalCompanyVO);
        ExcelUtil<HoGlobalCompanyVO> util = new ExcelUtil<>(HoGlobalCompanyVO.class);
        util.exportExcel(response, list, "全球分布导出");
    }


    /**
     * @description: 新增全球分布
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [globalCompany]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.GLOBAL_DISTRIBUTION,business = EOperationBusiness.GLOBAL_DISTRIBUTION, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoGlobalCompany globalCompany) {

        if(StringUtils.isNull(globalCompany.getRegion())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_REGION_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getPkCountry())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PKCOUNTRY_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getCompany())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_COMPANY_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getContacts())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_CONTACTS_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getPhone())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PHONE_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getGloProvince())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GLO_PROVINCE_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getGloCity())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GLO_CITY_NOT_NULL));
        }
        globalCompany.setPkCreator(SecurityUtils.getUserId());
        globalCompany.setCreationTime(new Date());
        return toAjax(globalCompanyService.saveCompany(globalCompany));
    }


    /**
     * @description: 修改全球分布
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [globalCompany]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.GLOBAL_DISTRIBUTION,business = EOperationBusiness.GLOBAL_DISTRIBUTION, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoGlobalCompany globalCompany) {
        if(StringUtils.isNull(globalCompany.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getRegion())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_REGION_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getPkCountry())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PKCOUNTRY_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getCompany())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_COMPANY_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getContacts())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_CONTACTS_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getPhone())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PHONE_NOT_NULL));
        }
        if(StringUtils.isEmpty(globalCompany.getAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getGloProvince())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GLO_PROVINCE_NOT_NULL));
        }
        if(StringUtils.isNull(globalCompany.getGloCity())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.GLO_CITY_NOT_NULL));
        }
        globalCompany.setPkModified(SecurityUtils.getUserId());
        globalCompany.setModifiedTime(new Date());
        return toAjax(globalCompanyService.updateCompany(globalCompany));
    }


    /**
     * @description: 查询单条个全球分布
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.HO_GLOBAL_COMPANY, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        HoGlobalCompany globalCompany = globalCompanyService.getById(pkId);
        HoGlobalCompanyExt gloExt=BeanUtil.copyProperties(globalCompany, HoGlobalCompanyExt.class);
        QueryWrapper<BdContinent> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_ID", globalCompany.getRegion());
        BdContinent son = continentService.getOne(queryWrapper);
        gloExt.setParentRegion(son.getPkParentId());
        return AjaxResult.success(gloExt);
    }


    /**
     * @description: 删除全球分布
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.GLOBAL_DISTRIBUTION,business = EOperationBusiness.GLOBAL_DISTRIBUTION, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<HoGlobalCompany> 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(globalCompanyService.update(updateWrapper));
    }
}
