package com.yijia.integral.controller;

import java.util.List;

import com.yijia.common.core.domain.entity.SysDept;
import com.yijia.common.core.domain.model.LoginUser;
import com.yijia.common.utils.SecurityUtils;
import com.yijia.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yijia.common.annotation.Log;
import com.yijia.common.core.controller.BaseController;
import com.yijia.common.core.domain.AjaxResult;
import com.yijia.common.enums.BusinessType;
import com.yijia.integral.domain.IntegralRule;
import com.yijia.integral.service.IIntegralRuleService;
import com.yijia.common.utils.poi.ExcelUtil;
import com.yijia.common.core.page.TableDataInfo;

/**
 * 积分规则Controller
 *
 * @author yijia
 * @date 2021-02-24
 */
@RestController
@RequestMapping("/integral/rule")
public class IntegralRuleController extends BaseController
{
    @Autowired
    private IIntegralRuleService integralRuleService;
    @Autowired
    private ISysDeptService sysDeptService;
    /**
     * 查询积分规则列表
     */
    @GetMapping("/list")
    public TableDataInfo list(IntegralRule integralRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
//        SysDept dept =new SysDept();
//        dept.setDeptId(currentUser.getUser().getDeptId());
//        List<String> list = sysDeptService.selectDeptId(dept);
//        if(list!=null && list.size()>0){
//            integralRule.setStationIdList(list);
//            integralRule.setStationId(null);
//        }
        integralRule.setStationId(currentUser.getUser().getDeptId());
        startPage();
        List<IntegralRule> integralRuleList = integralRuleService.selectIntegralRuleList(integralRule);
        return getDataTable(integralRuleList);
    }

    /**
     * 查询积分规则明细
     * @param integralRule
     * @return
     */
    @GetMapping("/listRuleInfo")
    public TableDataInfo listRuleInfo(IntegralRule integralRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralRule.setStationId(currentUser.getUser().getDeptId());
//        SysDept dept =new SysDept();
//        dept.setDeptId(currentUser.getUser().getDeptId());
//        List<String> list = sysDeptService.selectDeptId(dept);
//        if(list!=null && list.size()>0){
//            integralRule.setStationIdList(list);
//            integralRule.setStationId(null);
//        }
        startPage();
        List<IntegralRule> integralRuleList = integralRuleService.listRuleInfo(integralRule);
        return getDataTable(integralRuleList);
    }

    @GetMapping("/listRuleOne")
    public AjaxResult listRuleOne(IntegralRule integralRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
//        SysDept dept =new SysDept();
//        dept.setDeptId(currentUser.getUser().getDeptId());
//        List<String> list = sysDeptService.selectDeptId(dept);
//        if(list!=null && list.size()>0){
//            integralRule.setStationIdList(list);
//            integralRule.setStationId(null);
//        }
        integralRule.setStationId(currentUser.getUser().getDeptId());
        IntegralRule rule = integralRuleService.selectIntegralRule(integralRule);
        return AjaxResult.success(rule);
    }
    /**
     * 导出积分规则列表
     */
    @Log(title = "积分规则", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(IntegralRule integralRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        SysDept dept =new SysDept();
        dept.setDeptId(currentUser.getUser().getDeptId());
        List<String> list = sysDeptService.selectDeptId(dept);
        if(list!=null && list.size()>0){
            integralRule.setStationIdList(list);
            integralRule.setStationId(null);
        }
        List<IntegralRule> integralRuleList = integralRuleService.selectIntegralRuleList(integralRule);
        ExcelUtil<IntegralRule> util = new ExcelUtil<IntegralRule>(IntegralRule.class);
        return util.exportExcel(integralRuleList, "rule");
    }

    /**
     * 获取积分规则详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(integralRuleService.selectIntegralRuleById(id));
    }

    /**
     * 新增积分规则
     */
    @Log(title = "积分规则", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody IntegralRule integralRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        integralRule.setStationId(currentUser.getUser().getDeptId());
        if(integralRule!=null && integralRule.getIntegralActivity()==null){
            integralRule.setIntegralActivity("0");
        }
        if(integralRule!=null &&integralRule.getIntegralFlag()!=null && !integralRule.getIntegralFlag().equals("")){
            SysDept dept = new SysDept();
            dept.setDeptId(currentUser.getUser().getDeptId());
            dept.setIntegralFlag(integralRule.getIntegralFlag());
            sysDeptService.updateDept(dept);
        }
        return toAjax(integralRuleService.insertIntegralRule(integralRule));
    }

    /**
     * 修改积分规则
     */
    @Log(title = "积分规则", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody IntegralRule integralRule)
    {
        LoginUser currentUser = SecurityUtils.getLoginUser();
        int i =0;
        if(integralRule!=null&&integralRule.getIntegralFlag()!=null && !integralRule.getIntegralFlag().equals("")){
            if(integralRule.getIntegralFlag().equals("1")){
                SysDept dept =new SysDept();
                dept.setDeptId(currentUser.getUser().getDeptId());
                dept.setIntegralFlag(integralRule.getIntegralFlag());
                integralRule.setStationId(currentUser.getUser().getDeptId());
                i = sysDeptService.updateDept(dept);
                if(integralRule!=null && integralRule.getIntegralActivity()==null){
                    integralRule.setIntegralActivity("0");
                    integralRule.setDatePickerTime("");
                    i= integralRuleService.updateIntegralRule(integralRule);
                }else if(integralRule!=null){
                    i= integralRuleService.updateIntegralRule(integralRule);
                }
            }else if(integralRule.getIntegralFlag().equals("0")){
                SysDept dept =new SysDept();
                dept.setDeptId(currentUser.getUser().getDeptId());
                dept.setIntegralFlag(integralRule.getIntegralFlag());
                i= sysDeptService.updateDept(dept);
            }
        }
        return toAjax(i);
    }

    /**
     * 删除积分规则
     */
    @Log(title = "积分规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(integralRuleService.deleteIntegralRuleByIds(ids));
    }
}
