package com.yiqi.web.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.system.domain.SysOrgPrintSetting;
import com.yiqi.system.domain.dto.SysOrgPrintSettingDTO;
import com.yiqi.system.domain.vo.SysOrgPrintSettingVO;
import com.yiqi.system.service.ISysOrgPrintSettingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

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

/**
 * 打印机设置Controller
 *
 * @author ruoyi
 * @date 2023-12-25
 */
@Api(tags = "管理后台 - 打印机设置")
@RestController
@RequestMapping(UrlConstants.managerApi + "/print/setting")
public class SysOrgPrintSettingController extends BaseController {
    @Autowired
    private ISysOrgPrintSettingService sysOrgPrintSettingService;

    /**
     * 获取打印机详情
     */
    @GetMapping("/getPrintInfo")
    @ApiOperation(value = "获取打印机详情")
    public R<List<SysOrgPrintSettingVO>> getPrintInfo() {
        List<SysOrgPrintSetting> list = sysOrgPrintSettingService.list(new QueryWrapper<SysOrgPrintSetting>().lambda()
                .eq(SysOrgPrintSetting::getOrgId, SecurityUtils.getLoginUser().getOrgId())
                .eq(SysOrgPrintSetting::getSourceType, SecurityUtils.getLoginUser().getSourceType()));
        if (CollUtil.isEmpty(list)){
            return R.ok(new ArrayList<>());
        }
        List<SysOrgPrintSettingVO> queryVOList = JSON.parseArray(JSON.toJSONString(list), SysOrgPrintSettingVO.class);
        return R.ok(queryVOList);
    }


    /**
     * 设置打印机
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/setPrint")
    @ApiOperation(value = "设置打印机")
    public R<Boolean> setPrint(@RequestBody List<SysOrgPrintSettingDTO> sysOrgPrintSettingDTOS) {
        if (CollUtil.isEmpty(sysOrgPrintSettingDTOS)){
            return  R.fail("请选择打印机");
        }
        List<SysOrgPrintSetting> printSettings = sysOrgPrintSettingService.list(new QueryWrapper<SysOrgPrintSetting>().lambda()
                .eq(SysOrgPrintSetting::getOrgId, SecurityUtils.getLoginUser().getOrgId())
                .eq(SysOrgPrintSetting::getSourceType, SecurityUtils.getLoginUser().getSourceType()));
        if (CollUtil.isNotEmpty(printSettings)){
            sysOrgPrintSettingService.remove(new QueryWrapper<SysOrgPrintSetting>().lambda()
                    .eq(SysOrgPrintSetting::getOrgId, SecurityUtils.getLoginUser().getOrgId())
                    .eq(SysOrgPrintSetting::getSourceType, SecurityUtils.getLoginUser().getSourceType()));
        }
        List<SysOrgPrintSetting> list = new ArrayList<>();
        for (SysOrgPrintSettingDTO dto : sysOrgPrintSettingDTOS) {
            SysOrgPrintSetting sysOrgPrintSetting = new SysOrgPrintSetting();
            sysOrgPrintSetting.setPrintName(dto.getPrintName());
            sysOrgPrintSetting.setPrintType(dto.getPrintType());
            sysOrgPrintSetting.setOrgId(SecurityUtils.getLoginUser().getOrgId());
            sysOrgPrintSetting.setSourceType(SecurityUtils.getLoginUser().getSourceType());
            sysOrgPrintSetting.buildCreateData();
            list.add(sysOrgPrintSetting);
        }
        return R.ok(sysOrgPrintSettingService.saveBatch(list));
    }
}
