package com.willalp.web.controller.canteen;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.page.TableDataInfo;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.StringUtils;
import com.willalp.common.utils.http.HttpUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import com.willalp.canteen.domain.HsGift;
import com.willalp.canteen.service.IHsGiftService;
import com.willalp.system.domain.SysConfig;
import com.willalp.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 礼品登记Controller
 *
 * @author songyu
 * @date 2022-03-03
 */
@RestController
@RequestMapping("/gift/gift")
public class HsGiftController extends BaseController {
    @Autowired
    private IHsGiftService hsGiftService;

    @Resource
    private ISysConfigService sysConfigService;

    @Resource
    private HttpUtils httpUtils;

    /**
     * 导出礼品登记列表
     */
    @GetMapping("/getNotice")
    public AjaxResult getNotice(){
        return AjaxResult.success(sysConfigService.selectConfigByKey("noticeText"));
    }

    /**
     * 公告状态
     */
    @GetMapping("/getNoticeStatus")
    public AjaxResult getNoticeStatus(){
        return AjaxResult.success(sysConfigService.selectConfigByKey("noticeStatus"));
    }

    /**
     * 设置公告状态
     */
    @GetMapping("/setNoticeStatus")
    public AjaxResult setNoticeStatus(String openStatus){
        SysConfig sysConfig = sysConfigService.selectConfigInfoByKey("noticeStatus");
        sysConfig.setConfigValue(openStatus);
        return AjaxResult.success(sysConfigService.updateConfig(sysConfig));
    }


    /**
     * 导出礼品登记列表
     */
    @GetMapping("/notice")
    public AjaxResult notice(String text) {
        String noticeText = sysConfigService.selectConfigByKey("noticeText");
        SysConfig dbConfig = new SysConfig();
        dbConfig.setConfigKey("noticeText");
        List<SysConfig> sysConfigs = sysConfigService.selectConfigList(dbConfig);
        if (0 == sysConfigs.size()) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setConfigKey("noticeText");
            sysConfig.setConfigName("积分兑换公告");
            sysConfig.setConfigType("Y");
            sysConfig.setConfigValue(text);
            return toAjax(sysConfigService.insertConfig(sysConfig));
        }else{
            sysConfigs.get(0).setConfigValue(text);
            return toAjax(sysConfigService.updateConfig(sysConfigs.get(0)));
        }
    }

    /**
     * 查询礼品登记列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HsGift hsGift) {
        startPage();
        List<HsGift> list = hsGiftService.selectHsGiftList(hsGift);
        return getDataTable(list);
    }

    /**
     * 查询礼品登记列表
     */
    @GetMapping("/bindingList")
    public TableDataInfo bindingList(HsGift hsGift) {
        startPage();
        List<HsGift> list = hsGiftService.bindingList(hsGift);
        return getDataTable(list);
    }

    /**
     * 导出礼品登记列表
     */
    @Log(title = "礼品登记", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsGift hsGift) {
        List<HsGift> list = hsGiftService.selectHsGiftList(hsGift);
        ExcelUtil<HsGift> util = new ExcelUtil<HsGift>(HsGift.class);
        return util.exportExcel(list, "礼品登记数据");
    }

    /**
     * 获取礼品登记详细信息
     */
    @GetMapping(value = "/{giftId}")
    public AjaxResult getInfo(@PathVariable("giftId") String giftId) {
        return AjaxResult.success(hsGiftService.getById(giftId));
    }

    /**
     * 新增礼品登记
     */
    @Log(title = "礼品登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsGift hsGift) {
        while (true) {
            String idStr = IdWorker.getIdStr();
            HsGift byId = hsGiftService.getById(Long.valueOf(idStr));
            if (null == byId) {
                hsGift.setGiftId(idStr);
                break;
            }
        }
        hsGift.setCreateBy(SecurityUtils.getUsername());
        hsGift.setCreateTime(DateUtils.getNowDate());
        //编号
        try {
            while (true) {
                String data = httpUtils.sendGet(httpUtils.HTTPS + httpUtils.host + ":" + httpUtils.port + httpUtils.path + "/get/gnb");
                Thread.sleep(50);
                if (!StringUtils.equals(data, "ERROR")) {
                    hsGift.setGiftNumber(data);
                    break;
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败，无法获取编号");
        }
        return toAjax(hsGiftService.save(hsGift));
    }

    /**
     * 修改礼品登记
     */
    @Log(title = "礼品登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsGift hsGift) {
        hsGift.setUpdateBy(SecurityUtils.getUsername());
        hsGift.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsGiftService.updateById(hsGift));
    }

    /**
     * 删除礼品登记
     */
    @Log(title = "礼品登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/{giftIds}")
    public AjaxResult remove(@PathVariable String[] giftIds) {
        return toAjax(hsGiftService.removeByIds(Arrays.asList(giftIds)));
    }
}
