package com.fs.qw.controller;

import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.ServletUtils;
import com.fs.common.utils.poi.ExcelUtil;
import com.fs.core.security.LoginUser;
import com.fs.core.web.service.TokenService;
import com.fs.qw.vo.SortDayVo;
import com.fs.sop.domain.QwSopTemp;
import com.fs.sop.domain.QwSopTempDay;
import com.fs.sop.params.QwSopShareTempParam;
import com.fs.sop.service.IQwSopTempService;
import com.fs.sop.vo.UpdateRedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * sop模板Controller
 *
 * @author fs
 * @date 2024-09-26
 */
@RestController
@RequestMapping("/qw/sopTemp")
public class QwSopTempController extends BaseController
{
    @Autowired
    private IQwSopTempService qwSopTempService;
    @Autowired
    private TokenService tokenService;
    /**
     * 查询sop模板列表
     */
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:list')")
    @GetMapping("/list")
    public TableDataInfo list(QwSopTemp qwSopTemp)
    {
        startPage();
//        List<QwSopTemp> list = qwSopTempService.selectQwSopTempList(qwSopTemp);
        List<QwSopTemp> list = qwSopTempService.selectQwSopTempListNew(qwSopTemp);
        return getDataTable(list);
    }

    /**
     * 导出sop模板列表
     */
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:export')")
    @Log(title = "sop模板", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QwSopTemp qwSopTemp)
    {
        List<QwSopTemp> list = qwSopTempService.selectQwSopTempList(qwSopTemp);
        ExcelUtil<QwSopTemp> util = new ExcelUtil<QwSopTemp>(QwSopTemp.class);
        return util.exportExcel(list, "sop模板数据");
    }

    /**
     * 获取sop模板详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(qwSopTempService.selectQwSopTempById(id));
    }
    /**
     * 新增sop模板
     */
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:add')")
    @Log(title = "sop模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QwSopTemp qwSopTemp){
        return toAjax(qwSopTempService.insertQwSopTemp(qwSopTemp));
    }

    /**
     * 修改sop模板
     */
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @Log(title = "sop模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QwSopTemp qwSopTemp)
    {

        SimpleDateFormat slf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        qwSopTemp.setUpdateTime(slf.format(new Date()));
        return toAjax(qwSopTempService.updateQwSopTemp(qwSopTemp));
    }

    /**
     * 删除sop模板
     */
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:remove')")
    @Log(title = "sop模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        ;
//        qwSopTempService.deleteQwSopTempByIds(ids);
        return toAjax(qwSopTempService.updateQwSopTempByIds(ids));
    }

    /** 分享sop模板 */
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:share')")
    @Log(title = "分享sop模板", businessType = BusinessType.DELETE)
    @PostMapping("/shareTemp")
    public AjaxResult shareTemp(@RequestBody QwSopShareTempParam param)
    {
        return toAjax(qwSopTempService.shareQwSopTemp(param));
    }



    @PreAuthorize("@ss.hasPermi('qw:sopTemp:add')")
    @Log(title = "sop模板", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addNew(@RequestBody QwSopTemp qwSopTemp){
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        qwSopTemp.setCreateBy(loginUser.getUser().getUserId().toString());
        int i = qwSopTempService.addNew(qwSopTemp);
        if(qwSopTemp.getSendType() == 11){
            new Thread(() -> qwSopTempService.createSopTempRules(qwSopTemp)).start();
        }
        return toAjax(i);
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @Log(title = "sop模板", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult updateNew(@RequestBody QwSopTemp qwSopTemp){
        int update = qwSopTempService.update(qwSopTemp);
        return toAjax(update);
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @Log(title = "sop模板规则", businessType = BusinessType.UPDATE)
    @PostMapping("/addOrUpdateSetting")
    public AjaxResult addOrUpdateSetting(@RequestBody QwSopTempDay day){
        return AjaxResult.success(qwSopTempService.addOrUpdateSetting(day));
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @Log(title = "sop模板规则", businessType = BusinessType.DELETE)
    @GetMapping("/delRules")
    public AjaxResult delRules(Long id){
        qwSopTempService.delRules(id);
        return toAjax(1);
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:list')")
    @GetMapping("/selectRulesInfo")
    public AjaxResult selectRulesInfo(Long id){
        return AjaxResult.success(qwSopTempService.selectRulesInfo(id));
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @PostMapping("/copyTemplate")
    public AjaxResult copyTemplate(@RequestBody QwSopTemp qwSopTemp){
        qwSopTempService.copyTemplate(qwSopTemp);
        return toAjax(1);
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @PostMapping("/sortDay")
    public AjaxResult sortDay(@RequestBody List<SortDayVo> list){
        qwSopTempService.sortDay(list);
        return toAjax(1);
    }
    @PreAuthorize("@ss.hasPermi('qw:sopTemp:edit')")
    @GetMapping("/dayList")
    public AjaxResult dayList(String id){
        return AjaxResult.success(qwSopTempService.dayList(id));
    }


    @GetMapping("/redList")
    public AjaxResult redList(String id){
        return AjaxResult.success(qwSopTempService.redList(id));
    }


    @PostMapping("/updateRedPackage")
    public AjaxResult updateRedPackage(@RequestBody UpdateRedVo data){
        qwSopTempService.updateRedPackage(data.getList());
        return AjaxResult.success();
    }
    @GetMapping("/getSelectableRange")
    public R getSelectableRange(){
        return R.ok().put("data", qwSopTempService.getSelectableRange());
    }
}
