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


import cn.hutool.core.bean.BeanUtil;
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.*;
import com.hzs.common.core.utils.StringUtils;
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.BdDeliveryCentre;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.service.IBdDeliveryCentreService;
import com.hzs.system.config.vo.BdDeliveryCentreNoPageVO;
import com.hzs.system.config.vo.BdDeliveryCentreVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 配送中心设置 前端控制器
 * </p>
 *
 * @author zhangjing
 * @since 2023-02-23
 */
@RestController
@RequestMapping("/manage/delivery-centre")
public class BdDeliveryCentreController extends BaseController {

    @Autowired
    private IBdDeliveryCentreService deliveryCentreService;
    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    /**
     * @description: 查询配送中心列表
     * @author: zhang jing
     * @date: 2023/2/27 17:59
     * @param: [deliveryCentreVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.DELIVERYCENTRE, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(BdDeliveryCentreVO deliveryCentreVO) {
        startPage();
        List<BdDeliveryCentreVO> listVO = deliveryCentreService.queryDeliveryCentreList(deliveryCentreVO);
        return getDataTable(listVO);
    }

    /**
     * @description: 配送中心下拉选
     * @author: zhang jing
     * @date: 2023/3/3 9:31
     * @param: [deliveryCentreVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.DELIVERYCENTRE, method = EOperationMethod.SELECT)
    @GetMapping("/listNopPage")
    public TableDataInfo listNopPage(BdDeliveryCentreVO deliveryCentreVO) {
        List<BdDeliveryCentreVO> listVO = deliveryCentreService.queryDeliveryCentreList(deliveryCentreVO);
        List<BdDeliveryCentreNoPageVO> list = new ArrayList<>();
        listVO.forEach(item -> list.add(BeanUtil.copyProperties(item, BdDeliveryCentreNoPageVO.class)));
        return getDataTable(list);
    }

    /**
     * @description: 新增配送中心
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [shop]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DELIVERYCENTRE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody BdDeliveryCentre deliveryCentre) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isEmpty(deliveryCentre.getDeliveryName())) {
            return AjaxResult.error(ConfigMsgConstants.DELIVERY_NAME_NOT_NULL);
        }
        if (StringUtils.isNull(deliveryCentre.getDeliveryGrade())) {
            return AjaxResult.error(ConfigMsgConstants.DELIVERY_GRADE_NOT_NULL);
        }
        if (StringUtils.isEmpty(deliveryCentre.getContacts())) {
            return AjaxResult.error(ConfigMsgConstants.CONTACTS_NOT_NULL);
        }
        if (StringUtils.isEmpty(deliveryCentre.getMobilePhone())) {
            return AjaxResult.error(ConfigMsgConstants.MOBILEPHONE_NOT_NULL);
        }
        if (StringUtils.isEmpty(deliveryCentre.getProvince())) {
            return AjaxResult.error(ConfigMsgConstants.GLO_PROVINCE_NOT_NULL);
        }
        if (StringUtils.isEmpty(deliveryCentre.getCity())) {
            return AjaxResult.error(ConfigMsgConstants.GLO_CITY_NOT_NULL);
        }
        if (StringUtils.isEmpty(deliveryCentre.getAddress())) {
            return AjaxResult.error(ConfigMsgConstants.HO_ADDRESS_NOT_NULL);
        }
        QueryWrapper<BdDeliveryCentre> queryWrapper = new QueryWrapper();
        queryWrapper.eq("PK_COUNTRY", SecurityUtils.getPkCountry());
        if (StringUtils.isNotNull(deliveryCentre.getDeliveryName())) {
            queryWrapper.eq("DELIVERY_NAME", deliveryCentre.getDeliveryName());
        }
        List<BdDeliveryCentre> list = deliveryCentreService.list(queryWrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return AjaxResult.error(ConfigMsgConstants.DELIVERY_NAME_REPEAT);
        }
        deliveryCentre.setPkCountry(pkCountry);
        deliveryCentre.setPkCreator(SecurityUtils.getUserId());
        deliveryCentre.setCreationTime(new Date());
        return toAjax(deliveryCentreService.save(deliveryCentre));
    }

    /**
     * @description: 修改配送中心
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [shop]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DELIVERYCENTRE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody BdDeliveryCentre deliveryCentre) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        if (StringUtils.isNull(deliveryCentre.getPkId())) {
            return AjaxResult.error(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT);
        }
        if (deliveryCentre.getUsageStatus().intValue() != EYesNo.YES.getIntValue()) {
            if (StringUtils.isEmpty(deliveryCentre.getDeliveryName())) {
                return AjaxResult.error(ConfigMsgConstants.DELIVERY_NAME_NOT_NULL);
            }
            if (StringUtils.isNull(deliveryCentre.getDeliveryGrade())) {
                return AjaxResult.error(ConfigMsgConstants.DELIVERY_GRADE_NOT_NULL);
            }
            if (StringUtils.isEmpty(deliveryCentre.getContacts())) {
                return AjaxResult.error(ConfigMsgConstants.CONTACTS_NOT_NULL);
            }
            if (StringUtils.isEmpty(deliveryCentre.getMobilePhone())) {
                return AjaxResult.error(ConfigMsgConstants.MOBILEPHONE_NOT_NULL);
            }
            if (StringUtils.isEmpty(deliveryCentre.getProvince())) {
                return AjaxResult.error(ConfigMsgConstants.GLO_PROVINCE_NOT_NULL);
            }
            if (StringUtils.isEmpty(deliveryCentre.getCity())) {
                return AjaxResult.error(ConfigMsgConstants.GLO_CITY_NOT_NULL);
            }
            if (StringUtils.isEmpty(deliveryCentre.getAddress())) {
                return AjaxResult.error(ConfigMsgConstants.HO_ADDRESS_NOT_NULL);
            }
        }
        deliveryCentre.setPkCountry(pkCountry);
        deliveryCentre.setPkModified(SecurityUtils.getUserId());
        deliveryCentre.setModifiedTime(new Date());
        return toAjax(deliveryCentreService.updateById(deliveryCentre));
    }

    /**
     * @description: 查询单条配送中心
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DELIVERYCENTRE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        if (StringUtils.isNull(pkId)) {
            return AjaxResult.error(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT);
        }
        BdDeliveryCentre deliveryCentre = deliveryCentreService.getById(pkId);
        return AjaxResult.success(deliveryCentre);
    }

    /**
     * @description: 删除配送中心
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.DELIVERYCENTRE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        if (StringUtils.isNull(pkId)) {
            return AjaxResult.error(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT);
        }
        BdDeliveryCentre deliveryCentre = deliveryCentreService.getById(pkId);
        if (StringUtils.isNotNull(deliveryCentre)) {
            if (deliveryCentre.getUsageStatus().intValue() == EYesNo.YES.getIntValue()) {
                return AjaxResult.error(ConfigMsgConstants.ENABLE_STATE_NOT_DELETE);
            }
            UpdateWrapper<BdDeliveryCentre> 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());
            deliveryCentreService.update(updateWrapper);
        }
        return toAjax(true);
    }


}
